Topics

6/recent/ticker-posts

String in C Part 2 coversions and types of Strings



     Strings in C Part 2:

Finding length of the string: Counting no of chars in a string char.

 

#include<stdio.h>

#include<conio.h>

main()

{

  char str[30];

  int i;

              printf("\nenter a string");

              scanf("%s",str);

              for(i=0;str[i]!='\0';i++);

 

              printf("length =%d",i);

  getch();

}


 

**Using pre-defined function.**

        strlen()   à is a function from <string.h> header file to find length of the string.

Eg:

char str[10]=”hello”;

int len;

len=strlen(str);

 

Copying strings :

 

#include<stdio.h>

#include<conio.h>

main()

{

  char str1[30],str2[30];

  int i;

              printf("\nenter a string");

              scanf("%s",str1);

             for(i=0;str1[i]!='\0';i++)

              {

                        str2[i]=str1[i];

              }

              str2[i]='\0';

 

              printf("string1  %s  and string2  %s",str1,str2);

  getch();

}

 


Using pre-defined function:

 

strcpy() à is pre-defined function from <string.h> header to copy the strings. It works like an assignment operator. here source value will be copied into destination.

 

strcpy(destination, source);

 Note : size of  both strings must be same.

Using function.

 

            #include<stdio.h>

            #include<string.h>

            #include<conio.h>

            main()

        {

              char str1[30],str2[30];

              int i;

              printf("\nenter a string");

              scanf("%s",str1);

 

              strcpy(str2,str1);

 

              printf("string  %s  and string2  %s",str1,str2);

          getch();

        }

 

 

Concatenation of two strings: It is a joining two strings.

 

#include<conio.h>

#include<stdio.h>

 

main()

{

  char str1[50],str2[50],str3[100];

  int i,j;

            printf("\nEnter string1");

              scanf("%s",str1);

              printf("\nEnter string2");

              scanf("%s",str2);

              /* copying str1  ---> str3 */

              for(i=0;str1[i]!='\0';i++)

              {

                        str3[i]=str1[i];

              }

              /* appending str2 --> str3   */

              for(j=0;str2[j]!='\0';j++,i++)

              {

                str3[i]=str2[j];

              }

              str3[i]='\0';

              printf("string1 = %s  & string2 = %s ",str1,str2);

              printf("Result string = %s ",str3);

getch();

}

 


strcat() à it is pre-defined function to concate the strings.

           

strcat(destination, source);

strcat() function goes to end destination string and removes null char ( ‘\0’ ) and then copies the source string into destination string.

 

 

#include<conio.h>

#include<stdio.h>

#include<string.h>

main()

{

  char str1[50],str2[50],str3[100];

  int i,j;

            printf("\nEnter string1");

              scanf("%s",str1);

              printf("\nEnter string2");

              scanf("%s",str2);

              /* copying str1  ---> str3 */

              strcpy(str3,str1);

 

              /* appending str2 --> str3   */

              strcat( str3,str2);

 

              printf( "The string1 = %s  & The string2 = %s ",str1,str2);

              printf("Result string = %s ",str3);

getch();

}



Comparison of strings: It is a comparison of values.

 

#include<stdio.h>

#include<conio.h>

main()

{

  char str1[30],str2[30];

  int i,flag=0;

            printf("\nEnter string1");

              scanf("%s",str1);

              printf("\nEnter string2");

              scanf("%s",str2);

              /* comparison */

 

              for(i=0; str1[i]!='\0'||str2[i]!='\0';i++)

              {

                        if( (str1[i]-str2[i])>0)

                        {

                                    flag=1;

                                    printf("\n %s is greater than %s ",str1,str2);

                                    break;

                        }

                        else if( (str1[i]-str2[i])<0 )

                        {

                                    flag=1;

                                    printf("\n %s greater than %s",str2,str1);

                                    break;

                        }

              }

 

              if(flag==0)

              {

                 printf("\n both are equal");

              }

  getch();

}

 



Using pre-defined function:

 

strcmp()-- > to compare two strings.

 

int n;

 

n=strcmp(string1,string2);

 

it returns three values

1)     0 if both strings are same.

2)     +ve value if string1  > string2

3)     –ve value  if string1< string2

 

 

Using function comparison of strings

#include<stdio.h>

#include<conio.h>

#include<string.h>

main()

{

  char str1[30],str2[30];

  int n;

            printf("\nEnter string1");

              scanf("%s",str1);

              printf("\nEnter string2");

              scanf("%s",str2);

              /* comparison */

              n=strcmp(str1,str2);

 

              if(n==0)

              {

                        printf("\n both are equal");

              }

              else if(n>0 )

              {

                        printf("\n %s is greater than %s ",str1,str2);

              }

              else

              {

                        printf("\n %s is greater than %s ",str2,str1);

              }

getch();

}



#include<stdio.h>

#include<conio.h>

#include<string.h>

main()

{

  char str1[30],str2[30];

  int n;

            printf("\nEnter string1");

              scanf("%s",str1);

              printf("\nEnter string2");

              scanf("%s",str2);

              /* comparison */

           

              if(strcmp(str1,str2)==0)

              {

                        printf("\n both are equal");

              }

              else if(strcmp(str1,str2)>0 )

              {

                        printf("\n %s is greater than %s ",str1,str2);

              }

              else

              {

                        printf("\n %s is greater than %s ",str2,str1);

              }

getch();

}


      

Note: The programs given here are pre compiled and the continuation is given or the basics part is given in the previous page


Post a Comment

0 Comments