?
Solved

C basic string handling

Posted on 2003-03-20
9
Medium Priority
?
256 Views
Last Modified: 2010-04-15
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
Comment
Question by:bass20
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 3

Expert Comment

by:marcjb
ID: 8173929
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
 
LVL 1

Author Comment

by:bass20
ID: 8174666
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
 
LVL 3

Accepted Solution

by:
marcjb earned 60 total points
ID: 8174983
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
Technology Partners: 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!

 
LVL 30

Expert Comment

by:Mayank S
ID: 8180396
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
 
LVL 1

Author Comment

by:bass20
ID: 8190292
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
 
LVL 1

Author Comment

by:bass20
ID: 8190296
Mayank: It was a mistake, if an admin could please delete the double post, I'd appreciatte it. Sorry!
0
 
LVL 1

Author Comment

by:bass20
ID: 8190302
Clear response, as usual around here :)
0
 
LVL 3

Expert Comment

by:marcjb
ID: 8190757
Glad to help, and good luck :)

Marc
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 8192838
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
Suggested Courses
Course of the Month10 days, 7 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question