Thread: string program

  1. #1
    Registered User Mohammad Ahiya's Avatar
    Join Date
    Feb 2016
    Posts
    1

    Post string program

    I have tried to solve the problem below
    but i can't seem to get it right
    help please



    Write a program in C that:

    • declares a string variable called buffer with maximum size 80
    • inputs a string from the keyboard into the buffer
    • modifies the string contained in buffer by replacing any vowels (upper or lower case 'a', 'e', 'i', 'o' or 'u') with a plus sign (+)
    • prints out the modified string
    • prints out the total number of vowels that were replaced

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Surely you are able to do the first two and second last one, right? Why not post what you have done thus far?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2016
    Posts
    1
    Im stuck on the same problem
    Ive defined the variable called buffer with a maximum of 80 and prints out what the user entered.
    Im stuck on the part where it changes what the user entered into '+' symbol.

    Any help? or guide?

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    How about breaking the modification step in multiple sub-steps? Like:
    *) Have a counter ready for the amount of vowels
    *) Iterate through all of the characters in buffer.
    **) Check to see if each of them is a vowel.
    ***) If yes, replace them with a plus sign and increment the counter.
    Is it really that hard?
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    Here is my code, it does not work, but maybe it will help you.

    I tried to debug it, the problem part will be the loop at the end.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(){
    char buffer[80];
    char word[80];
    char word2[80];
    scanf("%s",word);
    strcpy(buffer, word);
    int i;
    int counter=0;
    
    for(i=0;i<80;i++)
    {
    	word2[i]="";
    }
    
    
    for(i=0;i<strlen(buffer);i++)
    {
       if(buffer[i]=="a" || buffer[i]=="e" || buffer[i]=="i" || buffer[i]=="o" || buffer[i]=="u" || buffer[i]=="A" || buffer[i]=="E" || buffer[i]=="I" || buffer[i]=="O" || buffer[i]=="U")
    //   if(strcmp(buffer[i],"a")==0)
       {
           strcat(word2,"+");
           counter++;
       }else{
       	   strcat(word2,buffer[i]);
       }
    
    }
    
    
    printf("The modified string is %s",word2);
    printf("in total there was %d vowels.",counter);
    
    
    return 0;
    }

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by nerio View Post
    Here is my code, it does not work, but maybe it will help you.
    How is posting broken, incorrect code supposed to help?

  7. #7
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    @ Matticus : Maybe he understands what I do not and I understand what he does not. And from an incorrect code one can learn to identify mistakes. Maybe instead of writing it all from zero he can just identify a mistake.

    Where is a mistake in my code?

  8. #8
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by nerio View Post
    Where is a mistake in my code?
    Did you even try to compile it?

    Code:
    In function 'main':|
    main.c|16|warning: assignment makes integer from pointer without a cast|
    main.c|20|warning: comparison between signed and unsigned integer expressions|
    main.c|22|warning: comparison between pointer and integer|
    main.c|22|warning: comparison with string literal results in unspecified behavior|
    main.c|22|warning: comparison between pointer and integer|
    main.c|22|warning: comparison with string literal results in unspecified behavior|
    main.c|22|warning: comparison between pointer and integer|
    main.c|22|warning: comparison with string literal results in unspecified behavior|
    main.c|22|warning: comparison between pointer and integer|
    main.c|22|warning: comparison with string literal results in unspecified behavior|
    main.c|22|warning: comparison between pointer and integer|
    main.c|22|warning: comparison with string literal results in unspecified behavior|
    main.c|22|warning: comparison between pointer and integer|
    main.c|22|warning: comparison with string literal results in unspecified behavior|
    main.c|22|warning: comparison between pointer and integer|
    main.c|22|warning: comparison with string literal results in unspecified behavior|
    main.c|22|warning: comparison between pointer and integer|
    main.c|22|warning: comparison with string literal results in unspecified behavior|
    main.c|22|warning: comparison between pointer and integer|
    main.c|22|warning: comparison with string literal results in unspecified behavior|
    main.c|22|warning: comparison between pointer and integer|
    main.c|22|warning: comparison with string literal results in unspecified behavior|
    main.c|28|warning: passing argument 2 of 'strcat' makes pointer from integer without a cast|
    ..\include\string.h|41|note: expected 'const char *' but argument is of type 'char'|
    ||=== Build finished: 0 errors, 23 warnings ===|
    
    Quote Originally Posted by nerio View Post
    @ Matticus : Maybe he understands what I do not and I understand what he does not. And from an incorrect code one can learn to identify mistakes. Maybe instead of writing it all from zero he can just identify a mistake.
    While it's great you want to help, you should avoid posting complete solutions, as this robs the OP of a valuable learning experience. Additionally, you should avoid posting code riddled with mistakes, as this may confuse the OP.

    Posting code is not always a bad thing, and while occasional bugs may slip through, you should ensure any code you post compiles cleanly and is tested to ensure at least basic functionality is correct.

  9. #9
    Registered User
    Join Date
    Dec 2015
    Posts
    68
    Instruction says to modify(e.g destroy) buffer so why word and word2?
    So something like this
    Code:
    scanf("%s", &buffer); // store directly in to buffer
    
     switch(buffer[i]){ // take advantage that case without a break is a fall through
     case 'a': case 'e': case 'i': case 'o': case 'u':
     case 'A': case 'E': case 'I': case 'O': case 'U': buffer[i] = '+'; j++ ;break;
     default: break;
    Last edited by tonyp12; 02-29-2016 at 05:01 PM.

  10. #10
    Registered User
    Join Date
    Feb 2016
    Posts
    1
    Can someone please contact me or post me how to do this, i cannot do this, someone please?
    Which part do i replace with your suggestion?

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by frocat View Post
    Can someone please contact me or post me how to do this, i cannot do this, someone please?
    Which part do i replace with your suggestion?
    Please start a new thread for your own problem.

    If you haven't already, please read the homework policy. We are here to assist with problems you encounter, not to provide complete solutions.

    Try your best to figure it out, and when you get stuck, post your progress so far (using code tags) and ask specific questions (on your own thread) about where you're stuck.

  12. #12
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    In the code that I posted in post #5 there is a mistake in comparing chars. I find it pretty difficult to compare char variables.

    Code:
    if(buffer[i]=="a" || buffer[i]=="e" || buffer[i]=="i" || buffer[i]=="o" || buffer[i]=="u" || buffer[i]=="A" || buffer[i]=="E" || buffer[i]=="I" || buffer[i]=="O" || buffer[i]=="U")
    I know that there is a function in C that enables to compare two strings ( strcmp(s1, s2); ). What is the best way how to compare two char variables?

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nerio
    What is the best way how to compare two char variables?
    With operator==. The problem is that "a" is a string literal, not a character literal.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help on string program please
    By Ryan Alleyne in forum C Programming
    Replies: 4
    Last Post: 03-21-2013, 11:10 PM
  2. need help about string in c program
    By raihan004 in forum C Programming
    Replies: 2
    Last Post: 09-15-2012, 05:54 AM
  3. String Program
    By ishwariamca in forum C++ Programming
    Replies: 11
    Last Post: 09-17-2008, 11:32 PM
  4. Help with string program
    By Duskan in forum C Programming
    Replies: 8
    Last Post: 04-02-2007, 08:27 AM
  5. String program
    By howdy_doody in forum C Programming
    Replies: 2
    Last Post: 03-13-2005, 07:01 PM

Tags for this Thread