0% found this document useful (0 votes)
14 views

String Word Swap in C

Uploaded by

harikaku.2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

String Word Swap in C

Uploaded by

harikaku.2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <stdio.

h>

void swap_characters(char *a, char *b) {


char temp = *a;
*a = *b;
*b = temp;
}

void reverse_section(char *start, char *end) {


while (start < end) {
swap_characters(start, end);
start++;
end--;
}
}

void swap_words_in_string(char *str) {


char *start = str;
char *end = str;

while (*end != '\0') {


end++;
}
end--;

reverse_section(start, end);

char *word_start = NULL;


while (*start != '\0') {
while (*start == ' ' && *start != '\0') {
start++;
}
word_start = start;
while (*start != ' ' && *start != '\0') {
start++;
}
reverse_section(word_start, start - 1);
}
}

int main() {
char str[100];
scanf("%s",&str);
swap_words_in_string(str);
printf("Swapped string : %s\n", str);
return 0;
}

You might also like