-->

Lab work 5 (string)with case study programs

 

 Lab Work 5: String

  1. Write a C program to find the length of string without using built in function.

  2. Write a C program to reverse the given string without using built in function.

  3. Write a C program to concatenate two given string without using built in function.

  4. Write a C program for the following problems using built in functions.
    strlen(), strrev(), strcmp(),strcpy() and strcat()

  5. Write a C program to convert the given line of text into upper case.

  6. Write a C program to input line of text and count the number of digit, vowels, consonants, white spaces and other characters.

  7. Write a C program to swap two given strings.

  8. Write a C program to check whether the given string is palindrome or not.

  9. Write a C program to input the names of 10 students and sort them in alphabetical order. 

  10. Write a C program to  search a string from the given set of strings

-----------------------------------------------------------------------------------------------------------------------------------------
Ans:-
q1)Write a C program to find the length of string without using built in function.
ans:-
Algorithm:-
step 1: start
step 2:Declare a string variable st[100],integer variable i,length
step 3:set length=0
step 4:Input a string for variable st
step 5:
        execute the loop from i=0 to \0 for given string st
            length=length+1
step 6:print length
step 7:stop
-------------------
//wap to find the lenght of given string
#include <stdio.h>

int main()
{
    char string[100];
    int count=0,i;
    printf("enter string\n");
    gets(string);
    for(i=0;string[i]!='\0';i++)
    {
        count++;
    }
    printf("length=%d",count);

    return 0;
}
Q2)Write a C program to reverse the given string without using built in function.
ans:-
Algorithm:-
step 1: start
step 2:Declare a string variable st[100],integer variable i,length
step 3:set length=0
step 4:Input a string for variable st
step 5:
        execute the loop from i=0 to \0 for given string st
            length=length+1
step 6:
         execute the loop from i=length to 0 for given string st
        print st with character
step 7:stop
///WAp to reverse a string without using strrev function().

#include <stdio.h>

int main()
{
    char string[100];
    int i;
    printf("enter string\n");
    gets(string);
    for(i=0;string[i]!='\0';i++);//no body line; takes the last value of i.
 
    for(int j=i;string[j]>=0;j--)//loop runs in reverse order.
    printf("%c",string[j]);//prints the characater.

    return 0;
}

q3)Write a C program to concatenate two given string without using built in function.
ans:-
Algorithm:-
step 1: start
step 2:Declare two strings variable st[100],st1[100],
step 3:Declare integer variable i,length
step 4:set length=0
step 5:Input strings for variable st,st1
step 6:
        execute the loop from i=0 to \0 for given string st
            length=length+1
step 7:
         execute the loop from i= 0 to \0 for given string st1
          let,st[length]=st1[i]
          let ,length=length+1  
step 8: add the null character \0 at the end of st variable
step 9:print st
step 10:stop
//WAP to concatenate  two strings  without using(strcat()) function.
#include <stdio.h>
#include<string.h>
int main()
{
    char st[100],st1[100];//two string declaration
    int i,j;
    printf("enter first string\n");
    
    scanf("%s",st); //it also CAN BE USED. IT accepts string with space until it meets new line
    printf("enter second string\n");
    scanf("%s",st1);
     for(i=0;st[i]!='\0';i++);//to get last value of i of first string
    for(j=0;st1[j]!='\0';j++)//loop execution till null character
    {

        st[i]=st1[j];//copying characters
        i++;
    }

    st[i]='\0';//adds an extra null character at the end.

       printf("concatenated string =%s\n",st);//prints the merged string
    return 0;
}
q4)Write a C program for the following problems using built in functions.
strlen(), strrev(), strcmp(),strcpy() and strcat()
ans:-
Algorithm:-
step 1: start
step 2:Declare a string variable st[100],integer variable i,length
step 3:Input a string for variable st
step 4:Find the length using strlen() for string st and store into length   
step 5:print length
step 6:stop
using strlen():-

//WAP to get /find length of string using(strlen()) function.

#include <stdio.h>
#include<conio.h>
int main()
{
    char name[100];//two dimensional declaration
    int i,count=0;              //declaration with initialization
    printf("enter string\n");
    gets(name);
    for(i=0;name[i]!='\0';i++)//loop execution till it reaches \0 character
    {
        count++;            //counting goes on
    }
    printf("total length=%d\n",count);//prints the length
    getch();
     return 0;
}
For strrev():-
Algorithm:-
step 1: start
step 2:Declare a string variable st[100]
step 3:Input a string for variable st
step 4:reverse the string using strrev() for string st
step 5:print st
step 6:stop
code:
//WAP to reverse a string using(strrev()) function.
#include <stdio.h>
#include<string.h>
int main()
{
    char name[100];//two dimensional declaration
    printf("enter string\n");
    //fgets(name, sizeof (name), stdin);
    scanf("%[^\n]s",name); //it also CAN BE USED. IT accepts string with space until it meets new line
    strrev(name);
    printf("reverse=%s\n",name);//prints the name
    return 0;
}
For strcmp():
Algorithm:-
step 1: start
step 2:Declare two strings variable st[100],st1[100]
step 3:Input string for variables st and st1
step 4: compare both strings st and st1 using strcmp() and store its value in k
step 5:
        if k>0
                print st has more ASCII value than st1
        else if k<0
                print st1 has more ASCII value than st
        else
                print both have same value
step 6:stop
"C" code:
//WAP to compare  two strings  using(strcmp()) function.
#include <stdio.h>
#include<string.h>
int main()
{
    char st[100],st1[100];//two string declaration
    printf("enter first string\n");
    gets(st);
    printf("enter second string\n");
     gets(st1);//inputs
    if(strcmp(st,st1)>0)//
    {
        printf("%s has more ascii value than %s",st,st1);
    }
    else if(strcmp(st,st1)<0)
    {
        printf("%s has more ascii value than %s",st1,st);

    }
    else
    {
        printf("%s and %s have same ascii value",st,st1);
    }
    return 0;
}
For strcpy():-
Algorithm:-
step 1: start
step 2:Declare strings variable st[100],st1[100]
step 3:Input a string for variable st
step 4:copy the the string st to st1 using strcpy(st1,st)
step 5:print st1
step 6:stop
code:
//WAP to copy  a string  using(strcpy()) function.
#include <stdio.h>
#include<string.h>
int main()
{
    char st[100],st1[100];//two string declaration
    printf("enter first string\n");
    scanf("%[^\n]s",st); //it also CAN BE USED. IT accepts string with space until it meets new line
    strcpy(st1,st);//copying a string into first
    printf("copied string =%s\n",st1);//prints the copied name
    return 0;
}

For strcat():-
Algorithm:-
step 1: start
step 2:Declare strings variable st[100],st1[100]
step 3:Input strings for variable st st1
step 4:merge the the string st with st1 using strcat(st1,st)
step 5:print st1
step 6:stop
code:
//WAP to merge  two strings  using(strcat()) function.
#include <stdio.h>
#include<string.h>
int main()
{
    char st[100],st1[100];//two string declaration
    printf("enter first string\n");
    gets(st);
    printf("enter second string\n");
     gets(st1);//inputs
    strcat(st,st1);//merging st1 string into first (st)
    printf("merged string =%s\n",st);//prints the merged strings(st)
    return 0;
}


q5)Write a C program to convert the given line of text into upper case.
ans:-
Algorithm:-
step 1: start
step 2:Declare strings variable st[100]
step 3:Input a string for variable st
step 4:convert the string into uppercase using strupr(st)
step 5:print st
step 6:stop
code:
//WAP to convert  a string into uppercase using(strupr()) function.
#include <stdio.h>
#include<string.h>
int main()
{
    char name[100];//declaration
    printf("enter string\n");
    
    scanf("%[^\n]s",name); //it also CAN BE USED. IT accepts string with space until it meets new line
    strupr(name);
    printf("string in uppercase=%s\n",name);//prints the name in uppercase
    return 0;
}

q6)Write a C program to input line of text and count the number of digit, vowels, consonants, white spaces and other characters.
ans:-
Algorithm:-
step 1: start
step 2:Declare a string variable st[100],integer variable i
step 3:set total_digit=0,total_vowels=0, total_consonants=0, total_white_spaces=0,total_other=0
step 4:Input a string for variable st
step 5:
        execute the loop from i=0 to \0 for given string st
            if st[i]>0 and st[i]<=9
                total_digit=total_digit+1
            elseif (st[i]>=65 and st[i]<=90) || (st[i]>=97 and st[i]<=122)
                    if st[i]=a or st[i]=e or st[i]=i or st[i]=o or st[i]=u
                            total_vowels=total_vowels+1
                    else
                            total_consonants=total_consonants+1
            elseif st[i]=' '
                    total_white_spaces=total_white_spaces+1
            else
                  total_other=total_other+1
     end of loop  
step 6:print  total_digit,total_vowels,total_consonants, total_white_spaces, total_other
step 7:stop

/*C program to input line of text and count the number of digit, vowels, consonants, white spaces and other characters. */ #include <stdio.h> #include<string.h> #include<ctype.h>> int main() { char string[100]; int i,total_digits,total_vowels,total_consonants,total_space,total_other; total_digits=total_vowels=total_consonants=total_space=total_other=0; printf("enter a string\n"); gets(string);//accepts string for(i=0;string[i]!='\0';i++)//loop execution till null character { if(string[i]>='0' && string[i]<='9') { total_digits++; } else if((string[i]>=65 && string[i]<=90)|| (string[i]>=97 && string[i]<=122)) { if(tolower(string[i])=='a' || tolower(string[i])=='e' || tolower(string[i])=='i' ||tolower(string[i])=='o' ||tolower(string[i])=='u') { total_vowels++; } else { total_consonants++; } } else if(string[i]==' ' )//condition testing for white space { total_space++; } else { total_other++; } } printf("total digits=%d,total vowels=%d,total consonants=%d,total spaces=%d,total other=%d\n", total_digits,total_vowels,total_consonants,total_space,total_other);// prints the total value. return 0; }
q7)Write a C program to swap two given strings.
ans:-
Algorithm:-
step 1: start
step 2:Declare strings variable st[100],st1[100] and t[100]
step 3:Input strings for variable st, st1
step 4:print st,st1
step 5: copy the string st to t variable
step 6:copy the st1 to st variable
step 7:copy the t to st1 variable
step 8:print st,st1
step 9:stop

code:-
//program to swap two strings
#include <stdio.h>
#include<string.h>
int main()
{
    char string[100],string1[100],tempo[100];
    printf("enter first string\n");
    gets(string);//accepts string
    printf("enter second string\n");
    gets(string1);
    printf("before swapping,strings are=%s and %s\n",string,string1);
    strcpy(tempo,string);//copies string to another
    strcpy(string,string1);//copies to string
    strcpy(string1,tempo);//copies string to string1

    printf("after swapping,strings are=%s and %s\n",string,string1);

    return 0;
}
q8)Write a C program to check whether the given string is palindrome or not.
ans:-
Algorithm:-
step 1: start
step 2:Declare strings variable st[100] and t[100]
step 3:Input strings for variable st
step 4: copy the string st to t variable
step 5:reverse the st1
step 6:compare both the strings t and st1 and store its value in k
step 7:
        if k=0
                print it is palindrome
        else
                print it is not palindrome
step 8:stop
//program to know a string is Palindrome or not
#include <stdio.h>
#include<string.h>
int main()
{
    char string[100],string1[100];
    printf("enter string\n");
    gets(string);//accepts string
    strcpy(string1,string);//copies string to another
    strrev(string);//reverses the string
    if((strcmp(string,string1)==0))//compares the strings are same or not
    {
        printf("it is palindrome");

    }
    else
    {
        printf("it is not palidrome");
    }


    return 0;
}
q9)Write a C program to input the names of 10 students and sort them in alphabetical order. 
ans:-
Algorithm:-
step 1: start
step 2:Declare string variables st[100][100], t[100],integer variable i,j
step 3:
          execute the loop from i=0 to 9
                input string st[i]
        end of loop   
step 4:
         execute the loop from i=0 to 9
                 execute the loop from j=i+1 to 9
                            compare strings st for location i and j and store the value  k
                                if k>0
                                      copy the string st for location i to t variable
                                       copy the string st for location j to  st for location i
                                        copy the string for t to st for location j
                                end of if
                end of loop
        end of loop
step 5:
          execute the loop from i=0 to 9
                print string st[i]
        end of loop 
step 6: stop
        
//WAP  to input some strings and sort them.
#include <stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
    char string[100][100],tempo[100];//two dimensional declaration
    int i,j,n;
    printf("enter n as total strings\n");
    scanf("%d",&n);
    printf("enter strings\n");
    for(i=0;i<=n-1;i++)
    {
        printf("string for location =%d \n",i);
        scanf("%s",string[i]);//gets inputs
    }
    printf("names and addresses are=\n");
    for(i=0;i<=n-1;i++)
    {
        printf("name =%s \n",string[i]);//prints the data .
    }
    printf("sorted strings are\n");
    for(i=0;i<=n-1;i++)
    {
        for(j=i+1;j<=n-1;j++)
        {
            if(strcmp(string[i],string[j])>0)//comparison of strings
            {
                strcpy(tempo,string[i]);
                strcpy(string[i],string[j]);//copy of string i.e. swapping of string
                strcpy(string[j],tempo);
            }
        }
    }
    printf("-------------------------------");
     for(i=0;i<=n-1;i++)
    {
        printf("name =%s \n",string[i]);//prints the data .
    }
getch();
     return 0;
}
q10)Write a C program to  search a string from the given set of strings
ans:-
step 1: start
step 2:Declare string variables st[100][100], t[100],integer variable i,total,flag=0
step 3:input total
step 4:
          execute the loop from i=0 to total-1
                input string st[i]
        end of loop   
step 5:input t(to be searched)
step 6:
         execute the loop from i=0 to total-1
                 
                            compare strings st for location i with string stored in t and store the value in k
                                if k=0
                                     flag=1
                                                exit the loop execution/stop
                                end of if
          end of loop
     
step 7:
          if flag=1
                print string found in location i
            else
                print string not found
            endif
step 8: stop
code:-
//program to search a string in given list of strings
#include <stdio.h>
#include<string.h>

int main()
{
    char string[100][100],st[100];
    int i,n,flag=0;
    printf("enter n\n");
    scanf("%d",&n);
 
    printf("enter strings\n");
 
    for(i=0;i<=n-1;i++)
    {
        scanf("%s",string[i]);
    }
    printf("enter string to be searched\n");
    scanf("%s",st);
    for(i=0;i<=n-1;i++)
    {
        if(strcmp(string[i],st)==0)
        {
            flag=1;break;
        }
    }
    if(flag==1)
    {
        printf("found and location=%d",i);
    }
    else
    {
    printf("not found");
    }
    return 0;
}
--------------------------------------------------------------------------------------------------------------
note:
You do not need to write comment/explanation given on right side.
case study:-
1. Write a menu driven program which reads given set of numbers in array and performs the following tasks.
    a)Find out the greatest one
    b)Find out the smallest one
    c)Print the numbers in ascending order
    d)Display the average of given numbers
    e)Exit
Ans:-
/* case study 1
Write a menu driven program which reads given set of numbers in array and performs the following tasks.
    a)Find out the greatest one
    b)Find out the smallest one
    c)Print the numbers in ascending order
    d)Display the average of given numbers
    e)Exit
*/
#include<stdio.h>
#include<stdlib.h>
int main()
{
    int numbers[100],i,j,k,total_data,greatest_no,smallest_no,sum=0;
    int choice;
    float average;
    printf("you have following menu\n");
    printf("--------------------------------\n");
    printf("1.to find the greatest one\n");
    printf("2.Find out the smallest one \n");
    printf("3.Print the numbers in ascending order\n");
    printf("4.Display the average of given numbers\n");
    printf("any other number to exit\n");
    printf("--------------------------------\n");
    printf("enter your choice\n");
    scanf("%d",&choice);
    switch(choice)
    {
                  case 1:
                       printf("we are going to find the greatest one\n");
                       printf("enter total data to be stored in an array\n");
                       scanf("%d",&total_data);
                       printf("enter elements of array\n");
                       for(i=0;i<total_data;i++)
                       {
                           scanf("%d",&numbers[i]);
                       }
                       greatest_no=numbers[0];
                       for(i=0;i<total_data;i++)
                       {
                          if(greatest_no<numbers[i])
                          {
                          greatest_no=numbers[i];
  }
                       }
                       
                       printf("greatest number is=%d\n",greatest_no);
                  break;
                  case 2:
                      printf("we are going to find the smallest one\n");
                       printf("enter total data to be stored in an array\n");
                       scanf("%d",&total_data);
                       printf("enter elements of array\n");
                       for(i=0;i<total_data;i++)
                       {
                           scanf("%d",&numbers[i]);
                       }
                       smallest_no=numbers[0];
                       for(i=0;i<total_data;i++)
                       {
                          if(smallest_no>numbers[i])
                          {
                          smallest_no=numbers[i];
  }
                       }
                       
                       printf("smallest number is=%d\n",smallest_no);
                  break;
                  case 3:
                       printf("we are going to print the numbers in ascending order\n");
                      
                       printf("enter total data to be stored in an array\n");
                       scanf("%d",&total_data);
                       printf("enter elements of array\n");
                       for(i=0;i<total_data;i++)
                       {
                           scanf("%d",&numbers[i]);
                       }
                       
                       for(i=0;i<total_data;i++)
                       {
                        for(j=i+1;j<total_data;j++)
                        {
 
                          if(numbers[i]>numbers[j])
                          {
                          k=numbers[i];
                          numbers[i]=numbers[j];
                          numbers[j]=k;
  }
}
                       }
                       printf("sorted numbers(in ascending order))=\n");
                       for(i=0;i<total_data;i++)
                       {
                           printf("%d\n",numbers[i]);
                       }
                       
                  
                  break;                         
                  case 4:     
                       printf("here,we are going to find average of numbers stored in an array\n");
                       printf("enter total data to be stored in an array\n");
                       scanf("%d",&total_data);
                       printf("enter elements of array\n");
                       for(i=0;i<total_data;i++)
                       {
                           scanf("%d",&numbers[i]);
                           sum=sum+numbers[i];
                       }
                       average=(float)sum/total_data;
                       
                       
                       printf("average number is=%f\n",average);
                  
                  break;
                  default:
                          printf("this choice is not in the list!\n");
                          printf("thank you!!!!!");
                          exit(0);
   
    return 0;
}
2.Write a menu driven program which reads a string from the user and performs the following tasks.
    a)converts the string into uppercase
    b)Find the length of string
    c)Reverse the  of string
    d)Check whether the string is Palindrome or not 
    e)Enter another string and compare it with the string
    f)Exit
 Ans:-
/* case study 1
Write a menu driven program which reads a string from the user and performs the following tasks.
    a)converts the string into uppercase
    b)Find the length of string
    c)Reverse the  of string
    d)Check whether the string is Palindrome or not 
    e)Enter another string and compare it with the string
    f)Exit
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
    char st[100],st1[100];
    int choice;
    
    printf("you have following menu\n");
    printf("--------------------------------\n");
    printf("1.to convert string into uppercase\n");
    printf("2.Find the length of string\n");
    printf("3.to reverse the string\n");
    printf("4.to check the string is Palindrome or not\n");
    printf("5.to compare two strings\n");
    printf("any other number to exit\n");
    printf("--------------------------------\n");
    printf("enter your choice\n");
    scanf("%d",&choice);
    fflush(stdin);//flushes the input buffer area
    switch(choice)
    {
                  case 1:
                       printf("we are going to convert string into uppercase\n");
                       printf("enter a string\n");
                       scanf("%[^\n]",st);
                      
   strupr(st);
                       
                       printf("string in capital letter=%s\n",st);
                  break;
                  case 2:
                      printf("we are going to find length of string\n");
                       printf("enter a string\n");
                       scanf("%[^\n]",st);
                      
                       
                       printf("length of string=%d\n",strlen(st));
                  break;
                  case 3:
                       printf("we are going to reverse the string \n");
                       printf("enter a string\n");
                       scanf("%[^\n]",st);
                       strrev(st);
                       
                       printf("reversed string =%s\n",st);
                       
                  
                  break;                         
                  case 4:   
  printf("we are going to test whether a string is Palindrome or not \n");
                        printf("enter first string\n");
                        scanf("%[^\n]",st);  
                        strcpy(st1,st);//copies string to another
    strrev(st);//reverses the string
    if((strcmp(st,st1)==0))//compares the strings are same or not
    {
         printf("it is palindrome");

    }
    else
    {
        printf("it is not palindrome");
    }
                break;
                  case 5:
                       printf("we are going to comapre two strings \n");
                       printf("enter first string\n");
                       scanf("%[^\n]",st);
                       fflush(stdin);
                       printf("enter second string\n");
                       scanf("%[^\n]",st1); 
                        if(strcmp(st,st1)>0)
    {
        printf("%s has more ASCII value than %s\n",st,st1);

    }
    else if(strcmp(st,st1)<0)
    {
    printf("%s has more ASCII value than %s\n",st1,st);
}
    else
    {
         printf("both strings have same ASCII value");
    }

                  
                  break;
                  default:
                          printf("this choice is not in the list!\n");
                          printf("thank you!!!!!");
                          exit(0);
   
    return 0;
}

No comments:

Post a Comment