News:

Choose a design and let our professionals help you build a successful website   - ITAcumens

Main Menu

String operations in infinite loop

Started by Kalyan, Apr 27, 2008, 10:42 PM

Previous topic - Next topic

Kalyan

String operations in infinite loop

#include
void string_copy(char *,char *);
void string_concat(char *,char *);
void string_word_count(char *,int *);
int string_length(char *);
int string_cmp(char *,char *);
main()
{
int option,count=0,x,y;
char str1[40],str2[40],strs1[40],strs2[40];
puts("enter the string:");
gets(str1);
while(1)
{
printf("1:for string copy, 2;string concatenate3:count string word count,4:strig length");
scanf("%d",&option);
switch(option)
{
case 1:
string_copy(str1,str2);
puts(str2);
break;
case 2:
string_concat(str1,str2);
puts(str1);
break;
case 3:
string_word_count(str1,&count);
printf("%d",count);
break;
case 4:
x=string_length(str1);
printf("%d",x);
break;
case 5:
puts("enter first string for compare:");
gets(strs1);
puts("enter second string for compare:");
gets(strs2);
y=string_cmp(strs1,strs2);
printf("%d",y);
break;
/* case 6:
string_char_str();
break;*/
case 7:
exit();
}
}
}
void string_copy(char *s1,char *s2)

{
while(*s1!='\0')
*s2++=*s1++;
*s2='\0';
}

void string_concat(char *s1,char *s2)
{
while(*++s1!='\0')
*s1=*s1;
puts(s2);
while(*s2!='\0')
{
puts(s2);
*s1++=*s2++;
}
*s1='\0';
}
//function for word length
void string_word_count(char *s1, int *cou)
{
while(*s1++!='\0')
{
if(*s1==32||*s1=='\0') // 32 in ascii equelent of ' '
{
*cou=*cou+1;
}
}
}
//function for calculate string length
int string_length(char *s1)
{
int cou=0;
while(*s1++!='\0')
{
cou++;
}
return cou;
}
//function for compare two strings
int string_cmp(char *s1,char *s2)
{
int co=0;
while(*s1++!='\0'&&*s2++!='\0')
{
co=*s1-*s2;
}
return co;
}