hey, this is an assignment for school, the total task of the program was to accept a string, print the an uppercase version ofthe same string, count the number of vowels in the string and finally copy the identified vowels to a seconday stringto be printed.
i have pretty much finished everything excpet for the copying of the vowels to the new string, i have a statement there but it doesnt od anyhting so i know thats where im going wrong. can anyone shed some light on my situation? thanks

int main()
    char str[50], newstr[50], vowstr[50];
    int vowels=0,len,i;
    for (int k=0;k<3;k++){
    printf("Enter the String:\n");

     int loc=0;  

  for (int j=0;j< strlen(str); j++){
      newstr[loc] = toupper(str[j]);

      newstr[loc] = '\0';

if(str[i]=='a'||str[i]=='A'||str[i]=='e'||str[i]=='E'||str[i]=='i'||str[i]=='I'||str[i]=='o'||str[i]=='O'||str[i]=='u'||str[i]=='U'){              //checking if it is vowel or not
    vowstr[i] = str[i];
    printf("\nThe uppercase version is : %s  \n", newstr);
    printf("\nTotal number of vowels : %d  \n\n",vowels);
    printf("the vowels encountered are : %s\n\n",vowstr);

   return 0;
4 Years
Discussion Span
Last Post by Adak

You can't use [i] as your index in vowstr[]. i is tied to the entire string that was entered.

BTW, you could have done all this in a single for loop. ;)

Add int m=0, and then use vowstr[m++] instead of vowstr[i], in the if statement


thank you @adak, that has fixed the problem with saving the vowels, however, now when its time to output it, im gettin only a few of the original vowels eneted but im also getting some weird characters ass2


The letters in vowstr[] are not a string - they're just a bunch of letters at this time. If you want to print them out as a string (or do any other string operation with them), you have to add the end of string marker char: '\0', just beyond the last letter in vowstr[].

If you want to enter another string, you need to first, set vowstr[0]='\0' (set it to NULL char).

Edited by Adak


would this work if i insert in right after line 27?

int len2= strlen(vowstr);
 vowstr[len2]= '\0';

Do that before you print up vowstr[]. Then, before you enter another string to be analyzed, you need to also set vowstr[] to "". Right now you are keeping the old vowels from the last string that was entered.

Just to be safe, maybe:


Then you know the last strings vowels are gone and can't zombie back to life.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.