Showing posts with label C - String. Show all posts
Showing posts with label C - String. Show all posts

Compairing Two String in C++ Program

#include <iostream>
using namespace std;
#include <cstring>
int main()
{
    char i[25]="Hello";
    char j[25]="World";
    char k[25]="Hello";
    cout<<"i is: "<<i<<endl;
    cout<<"j is: "<<j<<endl;
    strcat(i,j);
    cout<<"After Compairing:"<<endl;
    cout<<strcmp(i,j)<<endl;
    cout<<strcmp(i,k)<<endl;
    system("pause");
    return 0;
}


The output is given below:
i is: Hello
j is: World
After Compairing:
-1
1

Mesure the String Length

#include <stdio.h>
#include <string.h>
int main (void)
{
    char s1[100];
    char s2[100];
    gets(s1);
    gets(s2);
    printf("s1 = %d\ts2 = %d\n",strlen(s1),strlen(s2)); 
    system("pause");
    return 0;
}

Concatenate two C Strings

#include <stdio.h>
int main (void)
{
    char s1[100];
    char s2[100];
    gets(s1);
    gets(s2);
    strcat(s1,s2); //concatenate s1 and s2
    printf("s1= %s\n",s1);
    system("pause");
    return 0;
}

How to copy a C string

#include <stdio.h>
int main (void)
{
    char s1[100];
    char s2[100];
    gets(s1);
    gets(s2);
    strcpy(s1,s2); //copy s2 to s1
    printf("s1= %s s2= %s\n",s1,s2);
    system("pause");
    return 0;
}
    

Using C String by puts and gets function

#include <stdio.h>
#define CC "Please enter a string"
int main (void)
{
    char s[100];
    int i;
    puts(CC);
    gets(s);
    puts(s);
    system("pause");
    return 0;
}

How to write a C String

#include <stdio.h>
int main (void)
{
    char s[100];
    int i;
    printf("Enter a string: ");
    gets(s);
    for(i=0;s[i];i++)
    {
        printf("%c",s[i]);
    }
    system("pause");
    return 0;
}