Some string functions

puts()

in c i will implement.
Main job of this function is to print a new line autoamtically where printf() does not give this.

example:

#include<stdio.h>
int main()
{

    char string[]="This is an example of string";
    char *str1="This is string pointer";
    puts(string);
    puts("Hello string");
    puts(str1);
    return 0;
}

putchar()

#include<stdio.h>
int main()
{

    char string[]="This is an example of string";
    int i=0;
    system("cls");
    while(string[i]!='\0')
    {
        putchar(string[i]);
        i++;
    }
    return 0;
}
#include<stdio.h>
int main()
{
    char str[24]="AbCdEfGhIjK";
    int n;

    for(n=0;str[n]!='\0';n++)
    {
        putchar(str[n]);
    }
    return 0;
}

The actual work of putchar is it display string as character by character putchar is a macro that outputs character on stdout.

and the basic is : putchar(‘a’)    // it will print a
and putchar(a);     //it will print the value in stored in a
And the basic parameters of this is:
int putchar(int c);

Take input in string:

#include<stdio.h>
int main()
{
    char rakho[20];
    printf("city name:");
    scanf("%s",rakho);
    printf("So you live in %s",rakho);
    return 0;
}

In string we don’t mention ampersand & sign as string automatically take it as address.

catcpy.c

#include<stdio.h>
int main()
{
    char str1[]="Bangladesh plays party";
    char str2[]="Sad is a beautiful country";

    strcpy(str1+11,str2+4);
    strcat(str1,"! !! !!!");

    printf("%s",str1);


   return 0;
}

String equality:

#include<stdio.h>
#include<string.h>

int main()
{
    char str1[30],str2[30];
    int x;


printf("Enter first string:");
gets(str1);

printf("Enter second string:");
gets(str2);

x=strcmp(str1,str2);

if(0!=x)
{
    printf("Two strings are not equal\n");
}
else {
    printf("Two strings are equal\n");
}
getch();
    return 0;
}

String Lower:

#include<stdio.h>
#include<string.h>
int main()
{
char str1[]="LOWER case";
puts(strlwr(str1));
    return 0;
}

String Upper:

#include<stdio.h>
#include<string.h>
int main()
{
char str1[]="upper case";
puts(strupr(str1));
return 0;
}

strdup.c

#include<stdio.h>
#include<string.h>
int main()
{
    char *p1="Raja";
    char *p2;
    p2=strdup(p1);
    printf("Duplicated string is:%s",p2);
    return 0;
}

strstr.c

 

 

Reference for some examples:
http://fresh2refresh.com/c-programming/c-strings/

http://www.apiexamples.com/c/string/strstr.html

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published.