Problems Switching Words in a Sentence: Solved
Have you ever faced a problem where you needed to switch the positions of two words in a sentence, but couldn't figure out an easy way to do it? This article focuses on solving that problem by providing a C program that can switch any two words in a sentence. We will cover the key concepts of strings, pointers, and array manipulation in C.
Understanding Strings in C
A string in C is an array of characters, terminated by a null character ('\0'). The null character indicates the end of the string and is used by C functions to determine the length of the string. Here's an example of a string:
char recenica[] = "The quick brown fox jumps over the lazy dog.";In this example, the string "recenica" is an array of characters that contains the sentence "The quick brown fox jumps over the lazy dog.". The null character is automatically added after the last character 'g' in the array.
Manipulating Strings using Pointers
In C, strings can be manipulated using pointers. A pointer is a variable that stores the memory address of another variable. In strings, we can use pointers to access individual characters in the string. We can also use pointers to manipulate the contents of the string, such as switching the positions of two words.
Switching Words in a Sentence
To switch the positions of two words in a sentence, we need to follow these steps:
- Identify the starting positions and lengths of both words
- Create a temporary string to store one of the words
- Copy the content of one word to the starting position of the other word
- Copy the content of the temporary string to the starting position of the first word
C Program to Switch Words
Here's a C program that implements the above steps to switch the positions of two words in a sentence:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAKS_RIJECI 100
#define MAKS_DUZINA_RIJECI 50
char* ispremjestaj(char recenica[], int pozicije[2], int duzine[2]) {
char temp[MAKS_DUZINA_RIJECI];
int i;
// Copy the first word to the temporary string
for (i = 0; i < duzine[0]; i++) {
temp[i] = recenica[pozicije[0] + i];
}
temp[duzine[0]] = '\0';
// Copy the second word to the starting position of the first word
for (i = 0; i < duzine[1]; i++) {
recenica[pozicije[0] + i] = recenica[pozicije[1] + i];
}
// Copy the temporary string to the starting position of the second word
for (i = 0; i < duzine[0]; i++) {
recenica[pozicije[1] + i] = temp[i];
}
return recenica;
}
int main() {
char recenica[MAKS_RIJECI];
int pozicije[2];
int duzine[2];
printf("Unesite recenicu: ");
fgets(recenica, MAKS_RIJECI, stdin);
// Remove the newline character from fgets
for (int i = 0; recenica[i] != '\0'; i++) {
if (recenica[i] == '
') {
recenica[i] = '\0';
break;
}
}
printf("Unesite pozicije rijeci koje zelite zamijeniti: ");
scanf("%d%d", &pozicije[0], &pozicije[1]);
printf("Unesite duzine rijeci koje zelite zamijeniti: ");
scanf("%d%d", &duzine[0], &duzine[1]);
ispremjestaj(recenica, pozicije, duzine);
printf("Rezultat: %s
", recenica);
return 0;
}
Explanation of the C Program
The C program starts by including the necessary header files, defining macro constants for the maximum sentence length and maximum word length, and declaring a function called "ispremjestaj" that switches the positions of two words in a sentence. The function takes three arguments: a string containing the sentence, an array of two integers containing the starting positions of the two words, and an array of two integers containing the lengths of the two words.
The function creates a temporary string and copies the content of the first word to the temporary string. It then copies the content of the second word to the starting position of the first word. Finally, it copies the content of the temporary string to the starting position of the second word, effectively switching the positions of the two words.
The main function reads the sentence, the starting positions and lengths of the two words, and then calls the ispremjestaj function. It then prints the resulting sentence.
- Strings in C are arrays of characters terminated by a null character
- Strings can be manipulated using pointers in C
- The positions of two words in a sentence can be switched using a C program that copies the contents of the words to temporary strings and swaps their positions
References
- C Programming Language: https://www.cprogramming.com/
- String Manipulation in C: https://www.tutorialspoint.com/cprogramming/c_strings.htm
- String Functions in C: https://www.tutorialspoint.com/c_standard_library/string_h.htm