Reversing each word in a sentence
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
3
down vote
favorite
Below code reverse a word given by user input example.
input : This is code Review
output : siht si edoc weiveR
steps
step 1: check for whitespace if found set the start and end position and
pass to reverseword
step 2: check for null terminator and set start and end position pass to reverseword
#include<stdio.h>
#include <ctype.h>
#define MAX 100
#define SPACE 1
#define NEXT 1
#define MIN_CHAR 1
void reverseword(int start,int end,char *input)
char temp;
int i;
while(end > start)
temp=input[start];
input[start]=input[end];
input[end]= temp;
start++;
end--;
int main()
char input[MAX];
scanf("%[^n]",input);
int pos;
int start=0,end=0;
for(pos=0 ; input[pos] ; ++pos)
if(isspace(input[pos]) && pos>MIN_CHAR) // case when space is found
end = pos-SPACE;
reverseword(start,end,input);
start= pos+SPACE;
if(input[pos+NEXT]=='' && pos>MIN_CHAR) // case for last world
end =pos;
reverseword(start,end,input);
c strings
add a comment |Â
up vote
3
down vote
favorite
Below code reverse a word given by user input example.
input : This is code Review
output : siht si edoc weiveR
steps
step 1: check for whitespace if found set the start and end position and
pass to reverseword
step 2: check for null terminator and set start and end position pass to reverseword
#include<stdio.h>
#include <ctype.h>
#define MAX 100
#define SPACE 1
#define NEXT 1
#define MIN_CHAR 1
void reverseword(int start,int end,char *input)
char temp;
int i;
while(end > start)
temp=input[start];
input[start]=input[end];
input[end]= temp;
start++;
end--;
int main()
char input[MAX];
scanf("%[^n]",input);
int pos;
int start=0,end=0;
for(pos=0 ; input[pos] ; ++pos)
if(isspace(input[pos]) && pos>MIN_CHAR) // case when space is found
end = pos-SPACE;
reverseword(start,end,input);
start= pos+SPACE;
if(input[pos+NEXT]=='' && pos>MIN_CHAR) // case for last world
end =pos;
reverseword(start,end,input);
c strings
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
Below code reverse a word given by user input example.
input : This is code Review
output : siht si edoc weiveR
steps
step 1: check for whitespace if found set the start and end position and
pass to reverseword
step 2: check for null terminator and set start and end position pass to reverseword
#include<stdio.h>
#include <ctype.h>
#define MAX 100
#define SPACE 1
#define NEXT 1
#define MIN_CHAR 1
void reverseword(int start,int end,char *input)
char temp;
int i;
while(end > start)
temp=input[start];
input[start]=input[end];
input[end]= temp;
start++;
end--;
int main()
char input[MAX];
scanf("%[^n]",input);
int pos;
int start=0,end=0;
for(pos=0 ; input[pos] ; ++pos)
if(isspace(input[pos]) && pos>MIN_CHAR) // case when space is found
end = pos-SPACE;
reverseword(start,end,input);
start= pos+SPACE;
if(input[pos+NEXT]=='' && pos>MIN_CHAR) // case for last world
end =pos;
reverseword(start,end,input);
c strings
Below code reverse a word given by user input example.
input : This is code Review
output : siht si edoc weiveR
steps
step 1: check for whitespace if found set the start and end position and
pass to reverseword
step 2: check for null terminator and set start and end position pass to reverseword
#include<stdio.h>
#include <ctype.h>
#define MAX 100
#define SPACE 1
#define NEXT 1
#define MIN_CHAR 1
void reverseword(int start,int end,char *input)
char temp;
int i;
while(end > start)
temp=input[start];
input[start]=input[end];
input[end]= temp;
start++;
end--;
int main()
char input[MAX];
scanf("%[^n]",input);
int pos;
int start=0,end=0;
for(pos=0 ; input[pos] ; ++pos)
if(isspace(input[pos]) && pos>MIN_CHAR) // case when space is found
end = pos-SPACE;
reverseword(start,end,input);
start= pos+SPACE;
if(input[pos+NEXT]=='' && pos>MIN_CHAR) // case for last world
end =pos;
reverseword(start,end,input);
c strings
edited Jan 4 at 14:49
200_success
124k14143401
124k14143401
asked Jan 4 at 11:47
leuage
376
376
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
Enable warnings
The -Wall
on GCC leads to:
reverse.c:10:13: warning: unused variable âÂÂiâ [-Wunused-variable]
Do not repeat yourself
You check pos > MIN_CHAR
in two places. This could be done only once:
if (pos > MIN_CHAR)
if(isspace(input[pos])) // case when space is found
end = pos-SPACE;
reverseword(start,end,input);
start= pos+SPACE;
if(input[pos+NEXT]=='') // case for last world
end =pos;
reverseword(start,end,input);
Be consistent in your code style
The code style seems a bit inconsistent. For instance, sometimes you have whitespaces before "=", sometimes after, sometimes both, sometimes none... You'll find various coding styles online and different tools to check you code compliancy.
Declare variables in smallest possible scope
I find it a good habit to define variable in the smallest scope in order to make things easier to track. In your case, temp
could be declared inside the loop.
Getting rid of the your #define
-d values
All your values (except for MAX
) could be removed because the link between the name and the value is hard to understand at first.:w
So far, I've got:
#include<stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX 100
void reverseword(int start, int end, char *input)
while (end > start)
char temp = input[start];
input[start] = input[end];
input[end] = temp;
start++;
end--;
int main()
// Updated for testing purposes
// char input[MAX];
// scanf("%[^n]",input);
char * original = "this is code review";
char * input = strdup(original);
printf("%sn", input);
int pos;
int start = 0;
for (pos = 0 ; input[pos] ; ++pos)
if (pos > 1)
if (isspace(input[pos])) // case when space is found
int end = pos-1;
reverseword(start, end, input);
start = pos + 1;
if (input[pos+1] == '') // case for last world
int end = pos;
reverseword(start, end, input);
printf("%sn", input);
Typo: You meant "word", not "world"
Not much to add here.
Handle the last word at the end
You can handle the final case the end. Then you can merge the condition check togetherin the loop.
Bug found
We have the following behaviors:
- "a this is code review" -> "siht a si edoc weiver"
- " a this is code review" -> "a siht si edoc weiver"
I have to go, I'll try to continue asap.
Why don't you use a headline (# example
) instead of bold text?
â Zeta
Jan 4 at 23:53
Oops, no specific reason :-(
â Josay
Jan 5 at 9:06
add a comment |Â
up vote
2
down vote
Various problems exists with scanf("%[^n]",input);
It can over-fill leading to UB.
If the first character is a
'n'
,input
remains un-assigned leading to UB and'n'
still instdin
.A rare input error is not detected leading to subsequent UB.
On typical input,
'n'
remains instdin
, which could make a difference with a larger program.
Use fgets()
char input[MAX + 1];
if (fgets(input, sizeof input, stdin) == NULL) exit(EXIT_FAILURE);
input[strcspn(input, "n")] = ''; // lop off potential n
When ch < 0
is....(ch)
is undefined behavior. Best to make unsigned char
.
// if(isspace(input[pos]) && ....
if(isspace((unsigned char) input[pos]) && ....
With the context of OP's code with small strings, reverseword()
using int
is not a problem. For maximal functionality, C best uses size_t
for array indexing and so should reverseword()
. Recommend to define variables closer to where they are needed. int i;
is superfluous.
void reverseword(size_t start, size_t end, char *input)
while(end > start)
char temp = input[start];
input[start] = input[end];
input[end] = temp;
start++;
end--;
Inconsistent indentation implies OP is not using an auto-formatter to maintain a consistent style. Life is too short for manually maintaining the format. Use a programing environment that has an auto formatter and use it.
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
Enable warnings
The -Wall
on GCC leads to:
reverse.c:10:13: warning: unused variable âÂÂiâ [-Wunused-variable]
Do not repeat yourself
You check pos > MIN_CHAR
in two places. This could be done only once:
if (pos > MIN_CHAR)
if(isspace(input[pos])) // case when space is found
end = pos-SPACE;
reverseword(start,end,input);
start= pos+SPACE;
if(input[pos+NEXT]=='') // case for last world
end =pos;
reverseword(start,end,input);
Be consistent in your code style
The code style seems a bit inconsistent. For instance, sometimes you have whitespaces before "=", sometimes after, sometimes both, sometimes none... You'll find various coding styles online and different tools to check you code compliancy.
Declare variables in smallest possible scope
I find it a good habit to define variable in the smallest scope in order to make things easier to track. In your case, temp
could be declared inside the loop.
Getting rid of the your #define
-d values
All your values (except for MAX
) could be removed because the link between the name and the value is hard to understand at first.:w
So far, I've got:
#include<stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX 100
void reverseword(int start, int end, char *input)
while (end > start)
char temp = input[start];
input[start] = input[end];
input[end] = temp;
start++;
end--;
int main()
// Updated for testing purposes
// char input[MAX];
// scanf("%[^n]",input);
char * original = "this is code review";
char * input = strdup(original);
printf("%sn", input);
int pos;
int start = 0;
for (pos = 0 ; input[pos] ; ++pos)
if (pos > 1)
if (isspace(input[pos])) // case when space is found
int end = pos-1;
reverseword(start, end, input);
start = pos + 1;
if (input[pos+1] == '') // case for last world
int end = pos;
reverseword(start, end, input);
printf("%sn", input);
Typo: You meant "word", not "world"
Not much to add here.
Handle the last word at the end
You can handle the final case the end. Then you can merge the condition check togetherin the loop.
Bug found
We have the following behaviors:
- "a this is code review" -> "siht a si edoc weiver"
- " a this is code review" -> "a siht si edoc weiver"
I have to go, I'll try to continue asap.
Why don't you use a headline (# example
) instead of bold text?
â Zeta
Jan 4 at 23:53
Oops, no specific reason :-(
â Josay
Jan 5 at 9:06
add a comment |Â
up vote
4
down vote
accepted
Enable warnings
The -Wall
on GCC leads to:
reverse.c:10:13: warning: unused variable âÂÂiâ [-Wunused-variable]
Do not repeat yourself
You check pos > MIN_CHAR
in two places. This could be done only once:
if (pos > MIN_CHAR)
if(isspace(input[pos])) // case when space is found
end = pos-SPACE;
reverseword(start,end,input);
start= pos+SPACE;
if(input[pos+NEXT]=='') // case for last world
end =pos;
reverseword(start,end,input);
Be consistent in your code style
The code style seems a bit inconsistent. For instance, sometimes you have whitespaces before "=", sometimes after, sometimes both, sometimes none... You'll find various coding styles online and different tools to check you code compliancy.
Declare variables in smallest possible scope
I find it a good habit to define variable in the smallest scope in order to make things easier to track. In your case, temp
could be declared inside the loop.
Getting rid of the your #define
-d values
All your values (except for MAX
) could be removed because the link between the name and the value is hard to understand at first.:w
So far, I've got:
#include<stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX 100
void reverseword(int start, int end, char *input)
while (end > start)
char temp = input[start];
input[start] = input[end];
input[end] = temp;
start++;
end--;
int main()
// Updated for testing purposes
// char input[MAX];
// scanf("%[^n]",input);
char * original = "this is code review";
char * input = strdup(original);
printf("%sn", input);
int pos;
int start = 0;
for (pos = 0 ; input[pos] ; ++pos)
if (pos > 1)
if (isspace(input[pos])) // case when space is found
int end = pos-1;
reverseword(start, end, input);
start = pos + 1;
if (input[pos+1] == '') // case for last world
int end = pos;
reverseword(start, end, input);
printf("%sn", input);
Typo: You meant "word", not "world"
Not much to add here.
Handle the last word at the end
You can handle the final case the end. Then you can merge the condition check togetherin the loop.
Bug found
We have the following behaviors:
- "a this is code review" -> "siht a si edoc weiver"
- " a this is code review" -> "a siht si edoc weiver"
I have to go, I'll try to continue asap.
Why don't you use a headline (# example
) instead of bold text?
â Zeta
Jan 4 at 23:53
Oops, no specific reason :-(
â Josay
Jan 5 at 9:06
add a comment |Â
up vote
4
down vote
accepted
up vote
4
down vote
accepted
Enable warnings
The -Wall
on GCC leads to:
reverse.c:10:13: warning: unused variable âÂÂiâ [-Wunused-variable]
Do not repeat yourself
You check pos > MIN_CHAR
in two places. This could be done only once:
if (pos > MIN_CHAR)
if(isspace(input[pos])) // case when space is found
end = pos-SPACE;
reverseword(start,end,input);
start= pos+SPACE;
if(input[pos+NEXT]=='') // case for last world
end =pos;
reverseword(start,end,input);
Be consistent in your code style
The code style seems a bit inconsistent. For instance, sometimes you have whitespaces before "=", sometimes after, sometimes both, sometimes none... You'll find various coding styles online and different tools to check you code compliancy.
Declare variables in smallest possible scope
I find it a good habit to define variable in the smallest scope in order to make things easier to track. In your case, temp
could be declared inside the loop.
Getting rid of the your #define
-d values
All your values (except for MAX
) could be removed because the link between the name and the value is hard to understand at first.:w
So far, I've got:
#include<stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX 100
void reverseword(int start, int end, char *input)
while (end > start)
char temp = input[start];
input[start] = input[end];
input[end] = temp;
start++;
end--;
int main()
// Updated for testing purposes
// char input[MAX];
// scanf("%[^n]",input);
char * original = "this is code review";
char * input = strdup(original);
printf("%sn", input);
int pos;
int start = 0;
for (pos = 0 ; input[pos] ; ++pos)
if (pos > 1)
if (isspace(input[pos])) // case when space is found
int end = pos-1;
reverseword(start, end, input);
start = pos + 1;
if (input[pos+1] == '') // case for last world
int end = pos;
reverseword(start, end, input);
printf("%sn", input);
Typo: You meant "word", not "world"
Not much to add here.
Handle the last word at the end
You can handle the final case the end. Then you can merge the condition check togetherin the loop.
Bug found
We have the following behaviors:
- "a this is code review" -> "siht a si edoc weiver"
- " a this is code review" -> "a siht si edoc weiver"
I have to go, I'll try to continue asap.
Enable warnings
The -Wall
on GCC leads to:
reverse.c:10:13: warning: unused variable âÂÂiâ [-Wunused-variable]
Do not repeat yourself
You check pos > MIN_CHAR
in two places. This could be done only once:
if (pos > MIN_CHAR)
if(isspace(input[pos])) // case when space is found
end = pos-SPACE;
reverseword(start,end,input);
start= pos+SPACE;
if(input[pos+NEXT]=='') // case for last world
end =pos;
reverseword(start,end,input);
Be consistent in your code style
The code style seems a bit inconsistent. For instance, sometimes you have whitespaces before "=", sometimes after, sometimes both, sometimes none... You'll find various coding styles online and different tools to check you code compliancy.
Declare variables in smallest possible scope
I find it a good habit to define variable in the smallest scope in order to make things easier to track. In your case, temp
could be declared inside the loop.
Getting rid of the your #define
-d values
All your values (except for MAX
) could be removed because the link between the name and the value is hard to understand at first.:w
So far, I've got:
#include<stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX 100
void reverseword(int start, int end, char *input)
while (end > start)
char temp = input[start];
input[start] = input[end];
input[end] = temp;
start++;
end--;
int main()
// Updated for testing purposes
// char input[MAX];
// scanf("%[^n]",input);
char * original = "this is code review";
char * input = strdup(original);
printf("%sn", input);
int pos;
int start = 0;
for (pos = 0 ; input[pos] ; ++pos)
if (pos > 1)
if (isspace(input[pos])) // case when space is found
int end = pos-1;
reverseword(start, end, input);
start = pos + 1;
if (input[pos+1] == '') // case for last world
int end = pos;
reverseword(start, end, input);
printf("%sn", input);
Typo: You meant "word", not "world"
Not much to add here.
Handle the last word at the end
You can handle the final case the end. Then you can merge the condition check togetherin the loop.
Bug found
We have the following behaviors:
- "a this is code review" -> "siht a si edoc weiver"
- " a this is code review" -> "a siht si edoc weiver"
I have to go, I'll try to continue asap.
edited Jan 4 at 13:38
answered Jan 4 at 12:54
Josay
23.8k13580
23.8k13580
Why don't you use a headline (# example
) instead of bold text?
â Zeta
Jan 4 at 23:53
Oops, no specific reason :-(
â Josay
Jan 5 at 9:06
add a comment |Â
Why don't you use a headline (# example
) instead of bold text?
â Zeta
Jan 4 at 23:53
Oops, no specific reason :-(
â Josay
Jan 5 at 9:06
Why don't you use a headline (
# example
) instead of bold text?â Zeta
Jan 4 at 23:53
Why don't you use a headline (
# example
) instead of bold text?â Zeta
Jan 4 at 23:53
Oops, no specific reason :-(
â Josay
Jan 5 at 9:06
Oops, no specific reason :-(
â Josay
Jan 5 at 9:06
add a comment |Â
up vote
2
down vote
Various problems exists with scanf("%[^n]",input);
It can over-fill leading to UB.
If the first character is a
'n'
,input
remains un-assigned leading to UB and'n'
still instdin
.A rare input error is not detected leading to subsequent UB.
On typical input,
'n'
remains instdin
, which could make a difference with a larger program.
Use fgets()
char input[MAX + 1];
if (fgets(input, sizeof input, stdin) == NULL) exit(EXIT_FAILURE);
input[strcspn(input, "n")] = ''; // lop off potential n
When ch < 0
is....(ch)
is undefined behavior. Best to make unsigned char
.
// if(isspace(input[pos]) && ....
if(isspace((unsigned char) input[pos]) && ....
With the context of OP's code with small strings, reverseword()
using int
is not a problem. For maximal functionality, C best uses size_t
for array indexing and so should reverseword()
. Recommend to define variables closer to where they are needed. int i;
is superfluous.
void reverseword(size_t start, size_t end, char *input)
while(end > start)
char temp = input[start];
input[start] = input[end];
input[end] = temp;
start++;
end--;
Inconsistent indentation implies OP is not using an auto-formatter to maintain a consistent style. Life is too short for manually maintaining the format. Use a programing environment that has an auto formatter and use it.
add a comment |Â
up vote
2
down vote
Various problems exists with scanf("%[^n]",input);
It can over-fill leading to UB.
If the first character is a
'n'
,input
remains un-assigned leading to UB and'n'
still instdin
.A rare input error is not detected leading to subsequent UB.
On typical input,
'n'
remains instdin
, which could make a difference with a larger program.
Use fgets()
char input[MAX + 1];
if (fgets(input, sizeof input, stdin) == NULL) exit(EXIT_FAILURE);
input[strcspn(input, "n")] = ''; // lop off potential n
When ch < 0
is....(ch)
is undefined behavior. Best to make unsigned char
.
// if(isspace(input[pos]) && ....
if(isspace((unsigned char) input[pos]) && ....
With the context of OP's code with small strings, reverseword()
using int
is not a problem. For maximal functionality, C best uses size_t
for array indexing and so should reverseword()
. Recommend to define variables closer to where they are needed. int i;
is superfluous.
void reverseword(size_t start, size_t end, char *input)
while(end > start)
char temp = input[start];
input[start] = input[end];
input[end] = temp;
start++;
end--;
Inconsistent indentation implies OP is not using an auto-formatter to maintain a consistent style. Life is too short for manually maintaining the format. Use a programing environment that has an auto formatter and use it.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Various problems exists with scanf("%[^n]",input);
It can over-fill leading to UB.
If the first character is a
'n'
,input
remains un-assigned leading to UB and'n'
still instdin
.A rare input error is not detected leading to subsequent UB.
On typical input,
'n'
remains instdin
, which could make a difference with a larger program.
Use fgets()
char input[MAX + 1];
if (fgets(input, sizeof input, stdin) == NULL) exit(EXIT_FAILURE);
input[strcspn(input, "n")] = ''; // lop off potential n
When ch < 0
is....(ch)
is undefined behavior. Best to make unsigned char
.
// if(isspace(input[pos]) && ....
if(isspace((unsigned char) input[pos]) && ....
With the context of OP's code with small strings, reverseword()
using int
is not a problem. For maximal functionality, C best uses size_t
for array indexing and so should reverseword()
. Recommend to define variables closer to where they are needed. int i;
is superfluous.
void reverseword(size_t start, size_t end, char *input)
while(end > start)
char temp = input[start];
input[start] = input[end];
input[end] = temp;
start++;
end--;
Inconsistent indentation implies OP is not using an auto-formatter to maintain a consistent style. Life is too short for manually maintaining the format. Use a programing environment that has an auto formatter and use it.
Various problems exists with scanf("%[^n]",input);
It can over-fill leading to UB.
If the first character is a
'n'
,input
remains un-assigned leading to UB and'n'
still instdin
.A rare input error is not detected leading to subsequent UB.
On typical input,
'n'
remains instdin
, which could make a difference with a larger program.
Use fgets()
char input[MAX + 1];
if (fgets(input, sizeof input, stdin) == NULL) exit(EXIT_FAILURE);
input[strcspn(input, "n")] = ''; // lop off potential n
When ch < 0
is....(ch)
is undefined behavior. Best to make unsigned char
.
// if(isspace(input[pos]) && ....
if(isspace((unsigned char) input[pos]) && ....
With the context of OP's code with small strings, reverseword()
using int
is not a problem. For maximal functionality, C best uses size_t
for array indexing and so should reverseword()
. Recommend to define variables closer to where they are needed. int i;
is superfluous.
void reverseword(size_t start, size_t end, char *input)
while(end > start)
char temp = input[start];
input[start] = input[end];
input[end] = temp;
start++;
end--;
Inconsistent indentation implies OP is not using an auto-formatter to maintain a consistent style. Life is too short for manually maintaining the format. Use a programing environment that has an auto formatter and use it.
edited Jan 5 at 2:15
answered Jan 5 at 2:06
chux
11.4k11238
11.4k11238
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f184273%2freversing-each-word-in-a-sentence%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password