C code for concatenating strings

strCat: C code for user defined string concatenation of two strings
#include<stdio.h>
void strCat(char * str1, char *str2);
int main()
{
char str1[20];
char str2[20];
printf("Enter first string:\n");
gets(str1);

printf("Enter second string to concatenate to first:\n");
gets(str2);

strCat(str1,str2);
printf("Conacatenated String is:%s\n",str1);

return 0;
}

void strCat(char *p11, char *p22)
{
char *p1, *p2;
p1=p11;
p2=p22;

while(*p1)
p1++;

while(*p2)
{
*p1=*p2;
p2++;
p1++;
}
*p1='\0';


}
/*
Enter first string:
Hello India
Enter second string to concatenate to first:
How are you

Conacatenated String is:Hello IndiaHow are you

*/

C code for reversing a string

strRev: C code for user defined function to reverse a string

//strRev"User defined function to reverse a string
#include<stdio.h>
#include<string.h>
void strRev(char *arr1);

int main()
{
char arr1[20];
printf("Enter a string to be reversed:\n");
gets(arr1);
strRev(arr1);
printf("Reversed string is :%s\n",arr1);
return 0;
}

void strRev(char *arr1)
{
   char *begin;
   char *end;
   char temp;


   begin = arr1;
   end = arr1;

   int length, i;
   length=strlen(arr1);

 for ( i = 0 ; i < (length) - 1  ; i++ )
 end++;

   for ( i = 0 ; i < length/2 ; i++ )
   {
      temp = *end;
      *end = *begin;
      *begin = temp;

      begin++;
      end--;
   }
}

/*OUTPUT:

-bash-3.2$ ./a.out
Enter a string to be reversed:
hello
Reversed string is :olleh
-bash-3.2$ ./a.out
Enter a string to be reversed:
hello india
Reversed string is :aidni olleh
-bash-3.2$ ./a.out
Enter a string to be reversed:
a
Reversed string is :a
-bash-3.2$

*/

C code for copying one string to other

strCpy:C code for user defined function to copy one string to another
#include<stdio.h>
void strCpy(char *p2, char *p1);
int main()
{
char str1[20];
char str2[20];

printf("Enter a string:\n");
gets(str1);

strCpy(str2,str1);
printf("Copied string is:%s\n",str1);
return 0;
}

void strCpy(char *p2, char *p1)
{
while(*p1!='\0')
{
*p2=*p1;
p2++;
p1++;
}
*p2='\0';
}
/*
OUTPUT:
-bash-3.2$ gcc strCpy.c
/tmp/ccaP29bY.o: In function `main':
strCpy.c:(.text+0x24): warning: the `gets' function is dangerous and should not be used.
-bash-3.2$ ./a.out
Enter a string:
india
Copied string is:india
-bash-3.2$ ./a.out
Enter a string:
hello india
Copied string is:hello india
-bash-3.2$

*/

C code for copying one string to other

strCpy:C code for user defined function to copy one string to another
#include<stdio.h>
void strCpy(char *p2, char *p1);
int main()
{
char str1[20];
char str2[20];

printf("Enter a string:\n");
gets(str1);

strCpy(str2,str1);
printf("Copied string is:%s\n",str1);
return 0;
}

void strCpy(char *p2, char *p1)
{
while(*p1!='\0')
{
*p2=*p1;
p2++;
p1++;
}
*p2='\0';
}
/*
OUTPUT:
-bash-3.2$ gcc strCpy.c
/tmp/ccaP29bY.o: In function `main':
strCpy.c:(.text+0x24): warning: the `gets' function is dangerous and should not be used.
-bash-3.2$ ./a.out
Enter a string:
india
Copied string is:india
-bash-3.2$ ./a.out
Enter a string:
hello india
Copied string is:hello india
-bash-3.2$

*/

C code to find length of a string

strLen:c code for user defined function to find length of a string
#include<stdio.h>
int main()
{
char arr[20];
printf("Enter a string:\n");
gets(arr);

int result;
result=strLen(arr);
printf("Length of the string is:%d\n",result);
return 0;
}

int strLen(char *str)
{
char *tmp;
tmp=str;
while(*tmp)
{
tmp++;
}
return (tmp-str);
}
/*
-bash-3.2$ ./a.out
Enter a string:
India
Length of the string is:5
-bash-3.2$ ./a.out
Enter a string:
India is great
Length of the string is:14
*/


C code for comparing strings

strCmp:  c code for user defined function to compare two strings
//finding a string in strings array
#include<stdio.h>
int strStr(char *str1, char *str2);
int main()
{
char str1[100],str2[10];
printf("Enter few strings:\n");
gets(str1);
printf("Enter the string to search:\n");
gets(str2);

int result;
result=strStr(str1,str2);
if(result==0)
printf("Equals\n");
else
printf("NOT Equals\n");

//return 0;
}

int strStr(char *str1, char *str2)
{
while(*str1==*str2)
{
if(*str1=='\0' || *str2=='\0')
break;

str1++;
str2++;
}

if(*str1=='\0' && *str2=='\0')
return 0;
else
return -1;
}



/*
OUTPUT:
-bash-3.2$ gcc strCmp.c
/tmp/ccyAmuWc.o: In function `main':
strCmp.c:(.text+0x27): warning: the `gets' function is dangerous and should not be used.
-bash-3.2$ ./a.out
Enter few strings:
india
Enter the string to search:
india
Equals
-bash-3.2$ ./a.out
Enter few strings:
india
Enter the string to search:
in
NOT Equals
-bash-3.2$ ./a.out
Enter few strings:
in
Enter the string to search:
india
NOT Equals
-bash-3.2$ india
-bash: india: command not found
-bash-3.2$ ./a.out
Enter few strings:
India
Enter the string to search:
india
NOT Equals
-bash-3.2$

*/
//Alternatestrcmp: string compare -1,0,1
#include<stdio.h>
int strcompare(char *str1, char *str2);
int main()
{
char str1[20];
char str2[20];
printf("Enter String1:\n");
gets(str1);
printf("Enter String1:\n");
gets(str2);
int r;
r=strcompare(str1,str2);
if(r==0)printf("Equal\n");
else if(r>0)printf("Not Equal:%s comes after %s\n",str1,str2);
else printf("Not Equal:%s comes after %s\n",str2,str1);
return 0;
}
int strcompare(char *str1, char *str2)
{
while(((*str1-*str2)==0) && ( (*str1!='\0')|| (*str2!='\0')))
{
str1++;
str2++;
}
printf("*str1=%d,*str2=%d\n",*str1,*str2);
return(*str1-*str2);
}
/*
-bash-3.2$ ./a.out
Enter String1:
ind
Enter String1:
ind
Equal
-bash-3.2$ ./a.out
Enter String1:
ind
Enter String1:
india
Not Equal:india comes after ind
-bash-3.2$ ./a.out
Enter String1:
india
Enter String1:
ind
Not Equal:india comes after ind
-bash-3.2$ ./a.out
Enter String1:
india
Enter String1:
media
Not Equal:media comes after india
-bash-3.2$ ./a.out
Enter String1:
234
Enter String1:
123
Not Equal:234 comes after 123
-bash-3.2$ ./a.out
Enter String1:
india
Enter String1:
India
Not Equal:india comes after India
*/

C code to find a string

strStr: c code for user defined function to find a string from multiple strings
#include<stdio.h>

int compare_string(char*, char*);

int main()
{
    char first[100], second[100], result;

    printf("Enter first string\n");
    gets(first);

    printf("Enter second string\n");
    gets(second);

    result = compare_string(first, second);

    if ( result == 0 )
       printf("Both strings are same.\n");
    else
       printf("Entered strings are not equal.\n");

    return 0;
}

int compare_string(char *first, char *second)
{
   while(*first==*second)
   {
      if ( *first == '\0' || *second == '\0' )
         break;

      first++;
      second++;
   }
   if( *first == '\0' && *second == '\0' )
      return 0;
   else
      return -1;
}

/*
OUTPUT:
-bash-3.2$ vi strStr.c
-bash-3.2$ gcc strStr.c
/tmp/ccUUzk8E.o: In function `main':
strStr.c:(.text+0x27): warning: the `gets' function is dangerous and should not be used.
-bash-3.2$ ./a.out
Enter few strings:
all is well
Enter the string to search:
code
NOT Present
-bash-3.2$ ./a.out
Enter few strings:
all is well
Enter the string to search:
all
Present at location :1
-bash-3.2$ ./a.out
Enter few strings:
all is well
Enter the string to search:
is
Present at location :5
-bash-3.2$ ./a.out
Enter few strings:
all is well
Enter the string to search:
well
Present at location :8
-bash-3.2$ ./a.out
Enter few strings:
all is well
Enter the string to search:
ll
Present at location :2
-bash-3.2$
*/
//Alternatestrstr:String search return location
#include<stdio.h>
int strStrLocation(char *str1, char *str2);
int main()
{
char str1[20];
char str2[20];
printf("Enter String1:\n");
gets(str1);
printf("Enter String2:\n");
gets(str2);
int r=0;
r=strStrLocation(str1,str2);
if(r!=-1)printf("found at %d\n",r+1);
else printf("Not found\n");
return 0;
}
int strStrLocation(char *str11, char *str22)
{
char *str1;
char *str2;
str1=str11;
str2=str22;
int location=0;
while(*str11)
{
while(*str1==*str2)
{
str1++;
str2++;
if(*str1=='\0'||*str2=='\0') break;
}
if(*str2=='\0')
break;
str11++; location++;
str1=str11;
str2=str22;
}//first while
if(*str11)
{
printf("%c\n",*str11);
return location;
}
else
return -1;
}
/*
Enter String1:
india
Enter String2:
ind
i
found at 1
-bash-3.2$ ./a.out
Enter String1:
ind
Enter String2:
india
Not found
-bash-3.2$ ./a.out
Enter String1:
india is grt
Enter String2:
grt
g
found at 10
*/