Click here to Skip to main content
15,887,946 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
question is to create substrings of a string when we encounter space in it.

INPUT & OUTPUT
if input string is like "ram is raju" then output for this should be 3 as all the 3 substrings("ram","raju","is") are different and if input is like "ram is ram" then output should be 2 cause there only two different substrings "ram" and "is".

What I have tried:

this is my line of code for solving the problem.

but im unable to increase the counter from -1.
NOTE
i have started the counter from -1 cause when im comparing substrings for the first time then k's value will surely be not equal to 0 so as to bring the counter to zero i hve done this and due to my line of code i hve to compare e and d before copying d to e for the first time only.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
	int t,i=0,j,k,count=-1,l;
	char s[100],d[100],e[100];
	scanf("%d",&t);
	while(t>0)
	{
	 gets(s);
	 l=strlen(s);
	 //strcpy(e,"klo");
	 while(i<l)
 {
	 while((i<l)&&(s[i]==' '))
	 {i++;}
	 j=0;
	 while((i<l)&&(s[i]!=' '))
	 {
	     d[j]=s[i];
	     i++;
	     j++;
	 }
	 k=strcmp(e,d);
	 if(k!=0)
	 {count++;}
	 strcpy(e,d);
}
	printf("%d",count);
	t--;
	}
	return 0;
}
Posted
Comments
Member 13887543 26-Jun-18 16:30pm    
updated code
don't know why but it is showing segmentation error if i increase the value beyond 6 in this loop while(i<6)......for values less than 7 it os working fine


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
	char s[100],d[100],e[100];
	 long int g=0,l,i=0,j,k;
     gets(s);
     l=strlen(s);
     printf("%d\n",l);
  while(i<6)
  {
      while(s[i]==' ')
	 {i++;}
	 j=0;
      while(s[i]!=' ')
	 {
	     d[j]=s[i];
	     i++;
	     j++;
	 } 
	 d[j]='\0';
	 k=strcmp(d,e);
	if(k!=0)
	g++;
	printf("%d\n",g);
	 puts(d);
  }	 

	return 0;
}
Patrice T 26-Jun-18 18:02pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900