Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 259
  • Last Modified:

C basic string handling

Hello!

I need to do a quite simple thing in C; I have a string, and I have to add to it's end
the number of lowercase vowel sequences that end with an 'u'. For example,
"1ubAauedauB" turns into "1ubAauedauB2". Any hints?
Oh! And this is the 3rd function of a program; the previous 2 are already done and both return an altered string (as this one will). In the main() function, can I use something like: strcpy(output,conv3(conv2(conv1(input)))) to transfer the result of three conversions to an output string?

Thanx in advance! :)
0
bass20
Asked:
bass20
  • 4
  • 3
  • 2
1 Solution
 
marcjbCommented:
For adding on to the first string, use the 'strcat' function.  It will add one string onto another.

As for your second question,
strcpy(output,conv3(conv2(conv1(input))));
will work provided that:
1) conv1, conv2, and conv3 all take char* (an array of characters)
2) conv1, conv2, and conv3 all return a char*
3) input and output don't reference the same memory location (if they are 2 different strings, you are OK.  if they are pointers pointing at the same array, you are not allowed to do this.

Hope this helps,

Marc
0
 
bass20Author Commented:
Hum, I'll probably won't even have to use strcat as te inputted array will lose size allowing me to insert a new char at it's final index. My problem resides in how to detect the lowercase vowel sequences that end with an 'u'. As to the strcpy issue, thanks, I tought so :)
0
 
marcjbCommented:
The 'vowel' function below will do it.  It assumes that the string you pass is big enough to hold the extra characters.  I still used strcat, so that it would be easy to handle the cases where the orignal string had more than 9 occurences.

#include <stdio.h>

void vowel(char *s);

int main()
{
    char a[64] = "1ubAauedauB";
    vowel(a);
    printf("%s\n",a);
    return 0;
}

void vowel(char *s)
{
    char temp[10];
    int i, len, count;
    char previous;
   
    len = strlen(s);
    previous = s[0];
    count = 0;
    for ( i = 1; i < len; i++ ) {
        if ( s[i] == 'u' ) {
            switch(previous) {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
                count++;
                break;
            default:
                break;
            }
        }
        previous = s[i];
    }
    sprintf(temp, "%i", count);
    strcat(s, temp);
}

Another way to do this would be to use strstr and count the occurences of the 5 strings 'au', 'eu', 'iu', 'ou', and 'uu', but I think this is easier.

Hope this helps,

Marc
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mayank SAssociate Director - Product EngineeringCommented:
I see that you've posted the question twice.

Please refer:

http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20557404.html

Mayank.
0
 
bass20Author Commented:
marcjb: It worked perfectly, but I ended up doing like this:

char *convM(char *s){
     int i=0, j=1, contador=0;
     char aux[10];
     
      for(i=0, j=1; i<50; i++)
          {
          if(s[j]=='u')
               {
               if(isLowerVowel(s[i])!=0)
               contador++;
               }
          j++;
          }
         
     sprintf(aux, "%d", contador);
     strcat(s,aux);    
     return(s);

Wouldn't have done without your inspiration, tough! Thanks for the sprintf :))) Got all three string convertions working together perfectly, so thanks a lot! :))
0
 
bass20Author Commented:
Mayank: It was a mistake, if an admin could please delete the double post, I'd appreciatte it. Sorry!
0
 
bass20Author Commented:
Clear response, as usual around here :)
0
 
marcjbCommented:
Glad to help, and good luck :)

Marc
0
 
Mayank SAssociate Director - Product EngineeringCommented:
You have already accepted an answer but in case you want to try out another alternative, you can refer to:

http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20557404.html

Mayank.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now