Reputation: 19
I am new to the programming world and I'm trying to make a string split by using a loop that should split all characters separately but it's being ignored and basically ends with showing the input of the user instead of using the loop to separate the individual letters/characters. Have I missed declaring something important in the loop?
Upvotes: 1
Views: 67
Reputation:
If you're using java, you can just split with an empty string and then loop through the list that's created from the split method.
Upvotes: 0
Reputation: 1107
for (i = 0; str[i] != '\0'; i++);
<- there's a semicolon here, so your loop literally does nothing
also note that str[i] != '\0'
is a very dangerous way of iterating your string. If your string doesn't contain a zero-terminal character, C will happily continue reading memory beyond the end.
Upvotes: 2
Reputation: 254
There are few syntactical errors with what you posted.
/* This should be <stdio.h>
...
/* Don't need a. semi-colon here
int main();
...
/* Calling getchar() will cause you to lose the first character of the
the input
*/
getchar();
...
/* Don't need a semi-colon here */
for (i = 0; str[i] != '\0'; i++
...
system("pause");
}
With those adjustments you should find the code works.
Write text:
Hello world
Input: Hello world
H
e
l
l
o
w
o
r
l
d
sh: pause: command not found
I'm not on windows, so if the code on your end does not seem to work after making adjustments it is probably windows specific.
Upvotes: 0