Reputation:
I have a problem with my source code. When I do this, I have no output data, why ? How I can resolve this please ?
Thank's !
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void ft_split_whitespaces(char *str);
int main(int argc, char** argv)
{
ft_split_whitespaces("Hello\nThis is\ta\nWord");
return (0);
}
void ft_split_whitespaces(char *str)
{
int i;
for (i = 0; str[i] != '\0'; ++i);
{
if (str[i] == '\n' || str[i] == '\t' || str[i] == ' ')
{
printf("Ascii table (int): %d, Ascii table (char): %c\n", str[i], str[i]);
}
}
}
Upvotes: 0
Views: 103
Reputation: 100527
Typographical error - semicolon at the end of for
means empty statement - for (i = 0; str[i] != '\0'; ++i);
.
Fix: remove semicolon and never put one even instead of empty statement at the end of for
/while
/if
- use { /* intentionally empty */}
instead
for (i = 0; str[i] != '\0'; ++i) // no semicolon here ever.
{
if (str[i] == '\n' || str[i] == '\t' || str[i] == ' ')
{
printf("Ascii table (int): %d, Ascii table (char): %c\n", str[i], str[i]);
}
}
Upvotes: 2
Reputation: 747
What Alexei said.
To avoid that, I like to use this sort of curly bracing:
for (i = 0; str[i] != '\0'; ++i){
if (str[i] == '\n' || str[i] == '\t' || str[i] == ' '){
printf("Ascii table (int): %d, Ascii table (char): %c\n", str[i], str[i]);
}
}
This way the first thing you do after completing the conditions in a loop is put an open curly brace, instead of accidentally put a semicolon by reflex, and then jump down and put an open curly brace.
Upvotes: 3