+-+-+ +-+ +-+-+-+-+
|G|O| |4| |H|I|V|E|
+-+-+ +-+ +-+-+-+-+

 --- A GOPHER-LIKE INTERFACE FOR HIVE BLOCKCHAIN ---

Some common string problems with solutions in C

BY: @varunsangwan | CREATED: May 26, 2018, 1:16 p.m. | VOTES: 9 | PAYOUT: $0.18 | [ VOTE ]

[IMAGE: https://cdn.steemitimages.com/DQmSi9iwjLQDhm1GUgwQUWstH1Ln3BwgAjaswZJETrMopvk/strings.png]

Strings

A string is a type of data type Used in most of the programming languages it can store characters as well as integers. So basically It stores ASCII value of a given character or an integer in the memory. So today I will share some courts I've written just some basic ones Because I'm learning to programme right now in C to be precise I will be sharing some questions as well as answers to those questions now these answers are completely made by me so if you are looking for the optimised solution this blog is not for you I'm pretty sure there are some more optimised neat and clean codes than mine, But I am a beginner and I know there are other people that are starting with programming so this blog might be helpful for students like me. If you want to suggest any corrections or modifications in these codes that are highly appreciable. Without any further due that my first share that how a string works this includes how it is stored in memory etc. And please note that all the assumption, information and codes are discussed with respect to C language
[IMAGE: https://cdn.steemitimages.com/DQmarDpopBBtdfdr8SFHRDGeSRXkFZTA5QBRckfVYKKNiLY/sdfg.png]

How a string works

char str[100];

Question 1

Write a program to count total number of characters integers and special characters. In order to keep it simple special characters are everything other than characters and integers that includes (/,#,$,% etc.). No characters are basically A-Z and a-z, and Integers are simply (0,1,2,3,............)
Input | Output
---------|---------
Hello123# | Character- 5, Integer-3, Special Character- 1
ilove$teem!t | Character- 10, Integer-0, Special Character- 2

/Here's the answer :)

#include 

int main()
{
    char str[40];                                          //Declaration sf string
    scanf("%s",str);                                   // User enters string
    int i,alpha=0,chr=0,num=0;
    for(i=0;str[i]!='\0';i++)                        // Using Loop to determine Character, integer or speacial character.
    {
        if(str[i]>='a'&&str[i]<='z' || str[i]>='A'&&str[i]<='Z')
        {
            alpha++;
        }
        else if(str[i]>='0' && str[i]<='9')
        {
            num++;
        }
        else
        {
            chr++;
        }
    }
    printf("Characters-%d\nIntegers%d\nSpeacial Characters%d",alpha,num,chr);          // Printing the output
    return 0;
}

Question 2

Write a program to count the number of times vowels and consonants appear in a string, vowels are (a,e,i,o,u)

Input Output Hello vowel - 2, Consonant - 3 i love steemit vowel -6, Consonant - 6
#include 

int main()
{
    char str[40];             //Declaration of string
    scanf("%s",str);        // User enters string
    int i,vovel=0,conso=0;
    for(i=0;str[i]!='\0';i++)          // for loop to count vowels and consonants 
    {
        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' )
        {
            vovel++;
        }

        else
        {
            conso++;
        }
    }
    printf("Vovels = %d\nConsonats = %d",vovel,conso);
    return 0;
}

Question 3

Reverse given string.

Input Output Hello olleH i love steemit timeets evol i
#include 

int main()
{
    char str[100],temp;   //Declaring string
    gets(str);                        // Users enters string
    int i,j,a;
    a=strlen(str);                  //strlen gives the length of string
    j=a;
    for(i=0;i

int main()
{
    char str[100];   // Decleration of string 
    gets(str);               // User enters string
    int i,j,a,flag=0;
    a=strlen(str);             // Length of string 
    j=a;
    for(i=0;i

int main()
{
    char str[100];
    int m[100],i,j,n=0;
    gets(str);
    while(str[n]!='\0')
    {
        n++;
        m[n]=1;
    }
    for(i=0;i

int main()
{
    int m[100],i,j,n;              // Declerations 
    scanf("%d",&n);
    char str[n][50],temp[50];
    for(i=0;i0)
            {
                strcpy(temp,str[i]);                  //strcmp is a library function that returns the ascii diffrence between two strings
                strcpy(str[i],str[j]);
                strcpy(str[j],temp);
            }
        }
    }
    for(i=0;i<n;i++)
    {
        printf("%s\n",str[i]);
    }
    return 0;
}

That's it for now this might not be the best presentation but I tried my best I hope you really enjoyed this blog was probably haven't read that but anyways thanks for stopping by have a nice day see you later. :)

TAGS: [ #utopian-io ] [ #blog-quiz ] [ #programming ] [ #quiz ] [ #strings ]

Replies

NO REPLIES FOUND.

[ BACK TO TRENDING ] [ BACK TO MENU ]
CMD>