ADVERTISEMENT
ADVERTISEMENT

C string manipulation programs/examples (page 2)

List of solved examples/programs on string manipulation

6) C program to reverse string in same variable, without using another string variable to reverse.

This program will read a string and reverse string in same string variable. The logic behind to implement this program - swap fist and last character, second and second last character and so on.

/*C program to reverse string in same variable, 
without using  another string variable to reverse.*/

#include<stdio.h>
    
//Function to get string length
int stringLength(char str[])
{
    int len=0,i;
    for(i=0;str[i]!='\0';i++)
        len++;
    return len;
}
int main() 
{
    char text[100],tempChar;
    int i,length;
    
    printf("Enter any string: ");
    gets(text);
    
    printf("Entered String is: %s\n",text);
    
    //get string length
    length=stringLength(text);
    
    
    
    for(i=0;i<=(length/2);i++)
    {
        //swap elements
        tempChar=text[i];
        text[i]=text[length-i-1];
        text[length-i-1]=tempChar;
    }
    
    printf("Reverse string is: %s\n",text);
    return 0;
}
    Enter any string: www.includehelp.com
    Entered String is: www.includehelp.com
    Reverse string is: moc.plehedulcni.www

ADVERTISEMENT

7) C program to find occurrence of a character in the string.

This program will read a string and print the occurrence of entered character. Logic behind to implement this program - Read string and character to find, and compare each character of the string with entered character, if character matches with the string character then increase the counter. This process will repeat until null character not found. If counter’s value is non zero then print the occurrence otherwise character not found in the string.

/*C program to find occurrence of a character in the string.*/

#include<stdio.h>
    
int main()
{
    char text[100];
    int i,count;
    char ch; 
    
    printf("Enter any string: ");
    gets(text);
    
    printf("Enter character: ");
    scanf("%c",&ch);
    
    count=0;    //set counter with 0
    //start counting occurrence 
    for(i=0;text[i]!='\0';i++)
    {
        //compare character with string character
        if(text[i]==ch)
            count++;
    }
    
    if(count)
        printf("%c is found %d times in the string %s\n",ch,count,text);
    else
        printf("%c is not found in the string %s\n",ch,text);

    return 0;
}
Enter any string:Hello this is a programming learning website.  
Enter character: i 
i is found 5 times in the string Hello this is a programming learning website.

ADVERTISEMENT

8) C program to replace all vowels with star (*) and consonants with hash (#) of string.

This program will read string and replace all vowels with star (*) and hash (#) of entered string. Logic behind to implement - Read string, run loop from 0 to null, check for valid alphabet, if character is an alphabet, check for vowel and consonant and replace with star (*) and hash (#) respectively and then print the replaced string.

/*C program to replace all vowels with star (*) and 
consonants with hash (#) of string.*/

#include<stdio.h>
    
int main()
{
    char text[100];
    int i;
     
    printf("Enter any string: ");
    gets(text);
     
    printf("String is: %s\n",text);

    for(i=0; text[i]!='\0'; i++)
    {
        //check for alphabets
        if((text[i]>='A' && text[i]<='Z') || (text[i]>='a' && text[i]<='z'))
        {
            //check for vowels
            if(text[i]=='A' || text[i]=='a' || text[i]=='E' || text[i]=='e' || text[i]=='I' || text[i]=='i' || text[i]=='O' || text[i]=='o' || text[i]=='U' || text[i]=='u')
            {
                text[i]='*';
            }
            else
            {
                //otherwise alphabet is consonant
                text[i]='#';
            }
        }
    }

    printf("Replaced string is: %s\n",text);

    return 0;
}
    Enter any string: www.includehelp.com
    String is: www.includehelp.com 
    Replaced string is: ###.*###*#*#*##.#*#

ADVERTISEMENT

9) C program to print following pattern.


            H
            He
            Hel
            Hell
            Hello
/*C program to print following pattern.*/

#include<stdio.h>
    
//function to get string length
int stringLength(char text[])
{
    int length=0,i;
    
    for(i=0; text[i]!='\0'; i++)
        length++;
    return length;
}
int main()
{
    char string[]="Hello";
    int i,j,length;
    
    //get string length
    length=stringLength(string);
    
    //print string in patter
    for(i=0; i<=length ;i++)
    {
        /*run inner loop to print string characters
        in columns*/
        for(j=0; j<i; j++)
            printf("%c",string[j]);
        
        //print new line after each row
        printf("\n");
    }

    return 0;
}
    H
    He
    Hel
    Hell
    Hello



10) C program to print following pattern.


            Hello
            Hell
            Hel
            He 
            H
/*C program to print following pattern.*/

#include<stdio.h>
    
//function to get string length
int stringLength(char text[])
{
    int length=0,i;
    
    for(i=0; text[i]!='\0'; i++)
        length++;
    return length;
}
int main()
{
    char string[]="Hello";
    int i,j,length;
    
    //get string length
    length=stringLength(string);
    
    //print string in patter
    for(i=0; i<length ;i++)
    {
        /*run inner loop to print string characters
        in columns*/
        for(j=0; j<(length-i); j++)
            printf("%c",string[j]);
        
        //print new line after each row
        printf("\n");
    }

    return 0;
}
    Hello
    Hell
    Hel
    He 
    H



ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.