?
Solved

C basic string handling

Posted on 2003-03-20
3
Medium Priority
?
190 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
3 Comments
 
LVL 30

Accepted Solution

by:
Mayank S earned 60 total points
ID: 8179532
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
 
LVL 20

Expert Comment

by:jmcg
ID: 10189679
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

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!

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
Suggested Courses
Course of the Month11 days, 1 hour left to enroll

770 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