String exercise part -01

 


Problem 01: Write a program in C to concatenate two strings without using any library function.

Sample input: “My name ”  “is andy”

Sample output: “My name is andy”

Code: 

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

 void addstring (char result[], char str1[], char str2[])
 {
     int i;
     for(i=0;str1[i]!='\0'; i++)
     {
         result[i] = str1[i];
     }

     int j;
     for(j=0;str2[j]!='\0'; j++)
     {
         result[i+j] = str2[j];
     }

     result[i+j]='\0';
     return 0;
 }

 int main ()
 {
     char a[100], b[100], x[300];
     printf("Enter String: ");
     gets(a);
     printf("Enter String: ");
     gets(b);

     addstring(x,a,b);

     printf("\n%s",x);
     return 0;

 }


Problem 02: Write a program in C to count how many vowels are there in a string.

Sample Input: “My name is andy”      

Sample Output: 4

Code:

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

int countvowel (char str [])
{
    int i=0, vowel=0;
    while(str[i]!='\0')
    {
        if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||
           str[i]=='A'||str[i]=='E'||str[i]=='I'||str[i]=='O'||str[i]=='U')

        { vowel++; }

        i++;

    }

    return vowel;
}

 int main ()
 {
     char str[300];
     gets(str);
     printf("%d",countvowel(str));

     return 0;
 }


Problem 03: Write a program in C to count the number of words in a string.

Sample Input: “My name is andy”   

Sample Output:   4

Code:

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

 int countword (char str[])
 {
     int i=0,word=0;
     while(str[i]!='\0')
     {
         if(str[i]==' ' && str[i-1]!=' ')
         {
             word++;
         }
         i++;
     }

     return word+1;
 }

 int main()
 {
     char str[100];
     gets(str);
     printf("\n%d", countword(str));
     return 0;
 }


 

Problem 04: Write a program in C to convert lowercase string to uppercase.

Sample Input:   “My name is andy”    

Sample Output:    “MY NAME IS ANDY”

Code:

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

 void StrUppercase (char str[])
 {
     int i=0;
     while(str[i]!='\0')
     {
         if(str[i]>='a' && str[i]<='z')
         {
             str[i] = str[i]- 'a'+ 'A';
         }
         i++;
     }

 }

 int main ()
 {
     char str[300];
     gets(str);
     StrUppercase(str);
     puts (str);



     return 0;
 }


Problem 05: Write a program in C to count the occurrences of a character in a string regardless of its case.

Sample Input: “My name is Andy”  , ‘a’  

Sample Output: 2

 

Code:

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

int findx (char str[], char character)
{
int i=0,find=0;

while (str[i]!='\0')
{
if(str[i]==character || str[i] == character - 'a'+ 'A' || str[i] == character - 'A'+ 'a' )
{
find++;
}
i++;
}
return find;
}

int main ()
{
char str[300];
char x;
printf("Enter The String: ");
gets(str);
printf("Enter The Character: ");
scanf("%c", &x);

printf("%d",findx(str,x));


return 0;
}

  

See: String exercise part 02


 


 

 

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

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