String Exercise Part -02

 


Problem 06: Write a program in C to check whether a string is a palindrome or not.

Sample Input: “My name is andy”               “madam” 

Sample Output:    no                                        yes

Code:

 

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

int palindrome(char str1[]) {
int i, j;
int length = strlen(str1);

for (i = 0, j = length - 1; i < length; i++, j--)
{
if (str1[i] != str1[j])
{
return -1;
}
}

return 1;
}

int main() {

int check;
char string[500];
printf("Enter the string: ");
gets(string);

check = palindrome(string);

if (check == 1)
{
printf("\nYES, Palindrome");
}
else if (check == -1)

{
printf("\nNO, Not Palindrome");
}

return 0;
}


Problem 07: Write a program in C to add the digits in a string.

Sample Input: “I am 20 years old”    

Sample Output: 2

Code:

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

    int digitad (char str[])
    {
        int i, sum = 0;

        for(i=0; str[i]!='\0'; i++)
        {
            if(str[i]>='0' && str[i]<='9')
               {
                sum = sum+(str[i]-'0');
                //Converting the character of a digit to its integer value.

               }

        }

        return sum;
    }

    int main ()
    {
        char str[500];
        printf("Enter the string: ");
        gets(str);
        printf("\n Sum of the digit in the string = %d \n", digitad(str));


        return 0;
    }


  

Problem 08:  Write a program in C to count occurrences of a word in a string.

Sample Input: “I liked the story about the sad giant” , “the”   

Sample Output: 2

Code:

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

int  occar (char str1[], char str2[])
{
    int flag=0, found=0;
    int len1=strlen(str1);
    int len2=strlen(str2);
    for(int i=0; i<=len1-len2; i++)

    {
        flag=0;
        for(int j=0; j<len2; j++)
        {

            if(str1[i+j]== str2[j])

            {
                flag++;
            }

            else
            {
                flag=0;
                break;
            }

            if (flag == len2)
            {
                found++;
            }

        }
    }


    return found;

}

int main ()
{

    char str1[500];
    char str2[500];

    printf("Enter the 1st string: ");
    gets(str1);
    printf("Enter the 2nd string: ");
    gets(str2);
    printf("\n Found : %d \n", occar(str1, str2));

    return 0; 

}


Problem 09: Write a program in C to remove all repeated characters in a string.

Sample Input: “i like programming in C”     

Sample Output:   “i lkeprogamnC”

Code:

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

 void removeextra (char str[])
 {
     int i,j,k;

     for(i=0; str[i]!='\0';i++)
     {
          for(k=i+1, j=i+1; str[k]!='\0'; k++)
          {
              if(str[i]!=str[k])
              {
                  str[j]=str[k];
                  j++;
              }

          }
            str[j]='\0';
     }




 }

 int main()
 {
     char str[500];
     printf("Enter the sentence: ");
     gets(str);
     removeextra(str);
     puts(str);



     return 0;
 }


Problem 10: Write a program in C to find the maximum occurring character in a string.

Sample Input                                          Sample Output
“Welcome to CSE”        -------------------    E (or e)
“mmttssarrddd”             -------------------    D (or d)
“mmttssarrDDd”           -------------------    D (or d)

Code:

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

int maxoccur (char str[])
{
    int i,j,occur=0, max=0, maxindex;
    for(i=0; str[i]!='\0'; i++)
    {
        occur=0;
        for(j=i+1; str[j]!='\0'; j++)
        {
            if((str[i] - 'a' + 'A') ==str[j] || (str[i] - 'A' + 'a') == (str[j]))
            {
                occur++;

            }
            if (max<occur)
            {
                max= occur;
                maxindex= i;
            }
        }

    }

    return maxindex;

}

int main ()
{

    int x;
    char str[500];
    printf("Enter the string: ");
    gets(str);
    x= maxoccur (str);
    if(str[x]>= 'a' && str[x]<= 'z')
    {
        printf( "\n Max Occured charecter is: %c ( or %c ) \n", str[x]-'a'+'A', str[x]);

    }

    else if(str[x]>= 'A' && str[x]<= 'Z')
    {
        printf( "\n Max Occured charecter is: %c ( or %c ) \n",str[x],str[x]-'A'+'a');
    }

    return 0;
}

 

Problem 11:  Write a program in C to reverse the words in a string.

Sample Input:    “My name is Andy”    

Sample Output: “Andy is name My”

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

void wordswap(char str[])
{
    int i, j,temp;
    int len = strlen(str);


    for (i = 0; i < len/2 ; i++)
    {
        temp = str[i];
        str[i] = str[len - i - 1];
        str[len - i - 1] = temp;
    }

    int start = 0;
    for (int i = 0; i <= len; i++)
    {
        if (str[i] == ' ' || str[i] == '\0')
        {
            int end = i - 1;
            while (start < end)
            {
                char temp = str[start];
                str[start] = str[end];
                str[end] = temp;
                start++;
                end--;
            }
            start = i + 1;
        }
    }
}

int main()
{
    char str[500];
    printf("Enter the string: ");
    gets(str);
    wordswap(str);
    puts(str);

    return 0;
}

 

 See: String exercise part 01


 


নাফিজ বারাকাহ্

Content Creator, Writer, Designer. Writes technology based articles, poems & Product reviews. Create gameplay videos on youtube, designs Logo, Poster also Typography as a Hobby.

Post a Comment

Thank you for sharing your valuable comment.

Previous Post Next Post