• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 194
  • 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
1 Solution
 
Mayank SAssociate Director - Product EngineeringCommented:
Hi Bass!

It is always better not to return strings that way from functions because in case you're returning a local string, then by the time the control reaches back to main, your pointer variable (which receives the reutrned string) will be pointing to garbage (because memory for the local string will be deallocated once the function finishes execution).

Now, since arrays are always passed by address, so you can have the return type of the function to 'void' and simply receive the string in a character-pointer argument. Any modifications made by this function will then be permanent and automatically reflected in main ().

For your job, your conv3 () function can be written as:

void conv3 ( char * str )
{
  char temp[3] ;
  int count ;

// your code for counting the sequences - let 'count' hold the result
 
  itoa ( count, temp, 10 ) ; // converts count (base 10 - normal decimal value) to string form and stores in 'temp'

  strcat ( str, temp ) ; // concatenate to 'str'

} // end of conv3 ()

Now, 'str' holds the string that you want it to hold.

You can call your conversion-functions as:

void main ()
{
  char str[80] ;
..
..
  conv1 ( str ) ;
  conv2 ( str ) ;
  conv3 ( str ) ;
..
..

} // end of main ()


Hope that helps!

Mayank.
0
 
jmcgOwnerCommented:
Nothing has happened on this question in more than 9 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
delete, refund points (as requested by asker; mayankeagle raises a legitimate issue, but does not, in my opinion, fully resolve it).

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer
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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now