English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

//Prog to input a string & print each word along with the number of characters present in the string.

#include
#include
#include
#include
void main()
{
clrscr();
int i,l,k=0,l1;
char a[90],b[90];
cout< cout<<"Enter any string"< gets(a);
strcat(a," ");
l=strlen(a);
for(i=0;i {
if(a[i]!=' ')
{
b[k]=a[i];
k=k+1;
}
else
{
b[k]='\0';
cout< l1=strlen(b);
cout< k=0;
}
}
getch();
}
WHAT IS THE USE OF strcat(a," "); IN THIS PROG? what would have happend if it was not used? for which purpose is it used?

2007-01-06 02:20:53 · 3 answers · asked by priya . 1 in Computers & Internet Programming & Design

3 answers

they were meant to preserve the original string in case a had to be altered but here it is nothing more than repetiton and is not required..........
strcpy(c,"") copies blank string to c to remove garbage values
strcat concatenates the string a to c(same as strcpy here)

2007-01-06 21:12:58 · answer #1 · answered by preeti 2 · 0 0

The program prints each word in the string along with the length of that word. It does so by scanning the string and appending each character to a new string, until it finds a space. Then, it prints that string and starts over with a new string. The space at the end is to make sure it does this for the last word. Otherwise, it would just reach the end of the string and exit from the for loop naturally.

2007-01-06 11:27:55 · answer #2 · answered by watsonc64 3 · 0 0

I don't know C very well, but that strcat function just appends a space to the end of string a. If I'm correct, what the program does is stop reading the string as soon as a it reads a space. Which is sloppy programming, of course, as users could enter a space in the string read and it would stop halfway...
To illustrate, if you enter "example" as the string, the program would turn that into "example ", and stop reading as soon as it reads the space at the end of the string. That's how it knows how to stop, so it won't get an error. I think.

2007-01-06 10:28:45 · answer #3 · answered by jalexxi 3 · 0 0

fedest.com, questions and answers