C Programming/String Manipulation: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
No edit summary
 
(One intermediate revision by the same user not shown)
Line 63: Line 63:
   //string copy str1 into str3
   //string copy str1 into str3
   strcpy(str3, str1);
   strcpy(str3, str1);
   printf("strcpy( str3, str1) :  %s\n", str3 );
   printf("strcpy( str3, str1) :  %s\n", str3);


   //string concatenates str1 and str2
   //string concatenates str1 and str2
   strcat( str1, str2);
   strcat( str1, str2);
   printf("strcat( str1, str2) :   %s\n", str1 );
   printf("strcat( str1, str2) : %s\n", str1);


   //string lengh of str1
   //string lengh of str1
   len = strlen(str1);
   len = strlen(str1);
   printf("strlen(str1)        :  %d\n", len );
   printf("strlen(str1)        :  %d\n", len);


   return 0;
   return 0;
}
}
</syntaxhighlight>
</syntaxhighlight>

Latest revision as of 03:23, 5 February 2018

Equality Check

Case Sensitives

//main.c
#include <stdio.h>
#include <conio.h>
#include <string.h>

int main()
{
    char name[60];
    char halt[60] = "exit";
    for(int i=0;i<10;i++){
        printf("Enter your name: ");
        scanf("%s", name);

        if(strcmp(name, halt) == 0){
            return 0;
        }
        printf("Hello %s!\n", name);
    }
    return 0;
}

Ignore Case

//main.c
#include <stdio.h>
#include <conio.h>
#include <string.h>

int main()
{
    char name[60];
    char halt[60] = "exit";
    for(int i=0;i<10;i++){
        printf("Enter your name: ");
        scanf("%s", name);

        if(stricmp(name, halt) == 0){
            return 0;
        }
        printf("Hello %s!\n", name);
    }
    return 0;
}

String Utility

//main.c
#include <stdio.h>
#include <string.h>

int main ()
{
   char str1[12] = "Hello";
   char str2[12] = " World";
   char str3[12];
   int  len ;

   //string copy str1 into str3
   strcpy(str3, str1);
   printf("strcpy( str3, str1) :  %s\n", str3);

   //string concatenates str1 and str2
   strcat( str1, str2);
   printf("strcat( str1, str2) :  %s\n", str1);

   //string lengh of str1
   len = strlen(str1);
   printf("strlen(str1)        :  %d\n", len);

   return 0;
}