×

C Programs

C Basic & Conditional Programs

C Looping Programs

C String Programs

C Miscellaneous Programs

C string manipulation programs/examples without using Library Functions

String is the character array, usually we work with library functions to read and print the complete string but here you will find set of solved c programs/examples based on string manipulation. We will play with the strings and make all programs without using string.h header file.
Here we will access and manipulate each character from the character array (string) and then will print the string using our own method.

List of solved examples/programs on string manipulation

1) C program to print string one by one characters using loop.

As we know a string can be printed using printf("%s",string_variable) but in this program we will print string one by one character using loop. Here loop is running from 0 (access first character from character array) to Null (Null is the last character from character array - to terminate the string.)

/*C program to print string one by one characters using loop.*/

#include<stdio.h>
    
int main()
{
    char text[100];
    int i;
    
    printf("Enter any string: ");
    gets(text);
    
    printf("Entered string is: ");
    for(i=0;text[i]!='\0';i++)
    {
        printf("%c",text[i]);
    }
    printf("\n");
    return 0;
}
    Enter any string: www.includehelp.com
    Entered string is: www.includehelp.com



2) C program to print all VOWEL and CONSONANT characters separately.

In this program we will print all VOWEL and CONSONANT characters from entered string separately, logic behind to implement this program too easy, you have to just check if characters are vowels print them as vowels and not print them as consonants.

/*C program to print all VOWEL and CONSONANT characters separately.*/

#include<stdio.h>

int main()
{
    char text[100];
    int i;
    
    printf("Enter any string: ");
    gets(text);
    
    printf("String is: ");
    for(i=0;text[i]!='\0';i++)
    {
        printf("%c",text[i]);
    }
    printf("\n");
    
    printf("VOWEL Characters are: ");
    for(i=0;text[i]!='\0';i++)
    {
        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')
            printf("%c",text[i]);
    }
    printf("\n");
    
    printf("CONSONANT Characters are: ");
    for(i=0;text[i]!='\0';i++)
    {
        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'))
            printf("%c",text[i]);
    }
    printf("\n");
    
    return 0;
}
    Enter any string: www.includehelp.com 
    String is: www.includehelp.com  
    VOWEL Characters are: iueeo  
    CONSONANT Characters are: www.ncldhlp.cm 



3) C program to count upper case, lower case and special characters in a string.

In this program, we will count upper case, lower case and special characters. Logic behind to implement this program is too easy, just check characters is alphabet or not, if it is alphabet check for lower and upper case characters, if characters are not alphabets count them as special characters.

/*C program to count upper case, lower case and special characters in a string.*/

#include<stdio.h>

int main()
{
    char text[100];
    int i;
    int countL,countU,countS;
    
    printf("Enter any string: ");
    gets(text);

    //here, we are printing string using printf
    //without using loop
    printf("Entered string is: %s\n",text);
    
    //count lower case, upper case and special characters
    //assign 0 to counter variables
    countL=countU=countS=0; 
    
    for(i=0;text[i]!='\0';i++)
    {
        //check for alphabet
        if((text[i]>='A' && text[i]<='Z') || (text[i]>='a' && text[i]<='z'))
        {
            if((text[i]>='A' && text[i]<='Z'))
            {
                //it is upper case alphabet
                countU++;
            }
            else
            {
                //it is lower case character
                countL++;
            }
        }
        else
        {
            //character is not an alphabet
            countS++; //it is special character
        }
    }
    
    //print values
    printf("Upper case characters: %d\n",countU);
    printf("Lower case characters: %d\n",countL);
    printf("Special characters: %d\n",countS);
    
    return 0;
}
    Enter any string: Hello Friends, I am Mike. 
    Entered string is: Hello Friends, I am Mike.
    Upper case characters: 4  
    Lower case characters: 15 
    Special characters: 6 



4) C program to convert string in upper case and lower case.

In this program, we will convert string in upper case and lower case. First we will read a string and then convert string in upper case and after printing the string that is converted into upper case, we will convert it in lower case and print the lower case. Logic behind to implement this program - Just check the alphabets if they are in upper case add the value 32 [0x20 - hex value] to convert the character into lower case otherwise subtract 32 [0x20 - hex value] to convert the character into upper case.
Because difference between in the ascii codes of upper case and lower case characters are 32 [0x20 - hex value].

/*C program to convert string in upper case and lower case.*/

#include<stdio.h>

int main()
{
    char text[100];
    int i;
    
    printf("Enter any string: ");
    gets(text);
    
    printf("Entered string is: %s\n",text);
    
    //convert into upper case
    for(i=0;text[i]!='\0';i++)
    {
        if(text[i]>='a' && text[i]<='z')
            text[i]=text[i]-0x20;
    }
    printf("String in Upper case is: %s\n",text);
    
    //convert into lower case
    for(i=0;text[i]!='\0';i++)
    {
        if(text[i]>='A' && text[i]<='Z')
            text[i]=text[i]+0x20;
    }
    printf("String in Lower case is: %s\n",text);

    return 0;
}
    Enter any string: www.IncludeHelp.com
    Entered string is: www.IncludeHelp.com
    String in Upper case is: WWW.INCLUDEHELP.COM
    String in Lower case is: www.includehelp.com



5) C program to toggle case of all characters of string.

In this program, we will toggle all character’s case of string. Logic behind to implement this program is - Just convert lower case character in upper case and upper case character in lower case but first check that character is alphabet or not, if it is alphabet then do it.

/*C program to toggle case of all characters of string.*/

#include<stdio.h>

int main()
{
    char text[100];
    int i;
    
    printf("Enter any string: ");
    gets(text);
    
    printf("Entered string is: %s\n",text);
    
    //convert into upper case
    for(i=0;text[i]!='\0';i++)
    {
        //check character is alphabet or not
        if((text[i]>='A' && text[i]<='Z')||(text[i]>='a' && text[i]<='z'))
        {
            //check for upper case character
            if(text[i]>='A' && text[i]<='Z')
                text[i]=text[i]+0x20;
            else
                text[i]=text[i]-0x20;
        }
    }
    
    printf("String after toggle case: %s\n",text);

    return 0;
}
    Enter any string: www.IncludeHelp.com
    Entered string is: www.IncludeHelp.com
    String after toggle case: WWW.iNCLUDEhELP.COM



More String Examples



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.