1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Homework c programming

Discussion in 'C' started by prihuda22, Nov 21, 2014.

  1. prihuda22

    prihuda22 New Member

    Joined:
    Nov 21, 2014
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Create a C program that read two sentence

    1.For the second sentence, calculate how many
    a.Alphabet
    b.digits
    c.hexadigit
    2.Reverse the first sentence with converting its character
    a.From lower to upper
    B.From upper to lower
    3.Print string that consist of ten character from the first sentence and 8 character from the second sentence
    1416415196073.jpg
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Sounds easy enough. Where are you stuck? Do you understand the requirements?
     
  3. prihuda22

    prihuda22 New Member

    Joined:
    Nov 21, 2014
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    i understand just a little bit here my answer but just answer no 3
    #include <stdio.h>

    void concatenate_string(char*, char*);

    int main()
    {
    char original[100], add[100];

    printf("Enter first sentence\n");
    gets(original);

    printf("Enter second sentence\n");
    gets(add);

    concatenate_string(original, add);

    printf("String after concatenation is \"%s\"\n", original);

    return 0;
    }

    void concatenate_string(char *original, char *add)
    {
    while(*original)
    original++;

    while(*add)
    {
    *original = *add;
    add++;
    original++;
    }
    *original = '\0';
    }

    about no 1, and no 2 1 can't do it
     
  4. prihuda22

    prihuda22 New Member

    Joined:
    Nov 21, 2014
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    the last sentence is = about number 1,2 i can't do it
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Great! Did you run this code, and did it work? There is a small bug in that you don't test for string lengths, so if the combined string is too long then you will get a buffer overflow, but that's additional complexity you don't need to worry about (although extra points, if only from me, if you add a check for that).

    What do you mean "I can't do it"? Do you mean you don't understand the question? What have you tried, and what was the result?

    Having read the question I think it's a bit ambiguous. Does it mean to perform two reversals, or one reversal doing both case conversions at the same time? The latter I think is more likely, meaning given the string "Hello World" you would get the result "DLROw OLLEh"

    If you can't figure out how to reverse a string, imagine that you have a row of cards in front of you, and you need to reverse the order. Suppose you have Ace Jack Queen King, and you want King Queen Jack Ace. What would you do? Does that help work out how to reverse a string in a char array?
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Sorry, I just re-read the question (because I'd remembered there was a 3 after posting the above). Where does it ask you to concatenate the strings? This code might work perfectly but if it isn't what was asked for then you won't get marked very well.
     
  7. rehan_saiyad

    rehan_saiyad New Member

    Joined:
    Mar 12, 2015
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Trainee
    Location:
    Ahmedabad
    Code:
    #include<stdio.h>
    #include<string.h>
    void main()
    {	
    	
    	int i=0,alph=0,dig=0,b=0,l;
    	char str1[25],str2[25],temp[25],str3[25];
    	printf("\n\nEnter only alphabets and digit in string\n\n");		
    	printf("Enter 1st sentance : ");
    	scanf("%s",str1);
    	printf("Enter 2nd sentance : ");
    	scanf("%s",str2);
    	
    	while(str2[i] !='\0')
    	{	
    		printf("%c\t",str2[i]);
    		if(str2[i]>='a' && str2[i]<='z' || str2[i]>='A' && str2[i]<='Z')
    		{
    			alph++;
    		}
    		else if(str2[i]>='0' && str2[i]<='9')
    		{
    			dig++;
    		}
    		i++;
    
    	}
    	printf("\nNumber of alphabets are : %d\n",alph);
    	printf("\nNumber of digits are : %d\n",dig);
    		
    	
    	l=strlen(str1);
    	for(i=l-1;i>=0;i--)
    	{
    		temp[b]=str1[i]; //block for reverse string
    		b++;		//here characters are stored in temp[b] reversly
    	}
    	temp[b]='\0';
    	printf("Reversed string is : %s\n",temp);
    
    
    	for(i=0;i<=20;i++)//for loop that continues to the last character of the string
    	{
    		if(temp[i]>=65&&temp[i]<=90)//if uppercase
    		temp[i]=temp[i]+32;//adds to character to change lower case
    	
    		else if(temp[i]>=97&&temp[i]<+122)//if lowercase
    		temp[i]=temp[i]-32;//subtracts to change to uppercase
    	}
    	printf("\nConverted (upper character to lower & lower character to upper) string  is : %s\n",temp);
    	
    	for(i=0;i<10;i++)
    	{
    		str3[i]=temp[i];
    	}str3[i]='\0';		
    	
    	for(i=0;i<8;i++)
    	{
    		temp[i]=str2[i];
    	}
    	temp[i+1]='\0';
    		strcat(str3,temp);
    
    	printf("\nFinal concated string is :%s\n",str3);
    }
     
    Last edited by a moderator: Apr 5, 2015
  8. rehan_saiyad

    rehan_saiyad New Member

    Joined:
    Mar 12, 2015
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Trainee
    Location:
    Ahmedabad
    It works. Tihs code only for digits and alphabets it not count hex numbers.
     

Share This Page