Sunday, July 12, 2009

Help with c program?

i want to accept some text from the user and print it character by character .i wrote the below program.it is working for a single word but when the user enters more than one word with space inbetween,the strlen() function ignores words after space.how can i determine the tota number of characters in the text








#include%26lt;stdio.h%26gt;


void main()


{


char c[400];


int d,i;


printf("enter text \n");


scanf("%s",c);


d=strlen(c);


for(i=0;i%26lt;d;i++)


{


printf("%c\n",c[i]);


}


getch();


}

Help with c program?
just modify ur scanf fn to like this





scanf("%[^ \n\t]s",c);





thats it.
Reply:for this you have change like below:


you have to use gets() function for more than one word











#include%26lt;stdio.h%26gt;


#include%26lt;string.h%26gt;


void main()


{


char c[400];


int d,i;


printf("enter text \n");


gets(c);


d=strlen(c);


for(i=0;i%26lt;d;i++)


{


printf("%c\n",c[i]);


}


getch();


}
Reply:scanf("%s", c) doesn't get all the characters inputted by the user... If it sees a "space", then scanning of characters is already stopped... In C, there is a function which gets all the characters including spaces (except for newline character since it is the cue ending the function), it is gets() probably under stdlib.h or string.h, just find it out...





Anyway, to use it, here's how:





int main()


{


char c[400];


gets(c); //On this part, the program waits for input until Enter is pressed...


int len = strlen(c); //get the length of the string through strlen();


for (int i = 0; i %26lt; len; i++)


{


printf("%c", c[i]\n);


}


getch();


}





That is how you do it... I hope it helped.. (^^,)
Reply:Why is the printf in a for loop take the for loop out and replace it with printf("%s",c) and see what that does


been a while since I've done simple dos output like that but is should work
Reply:yaa dear I understand the Problem you select the data type Char with array. but u get data with Scan f who only get value before spce. U replace the Scan f Function with gets() functio which means get strings it take all the charachter with Spaces. ok.

mothers day flowers

No comments:

Post a Comment