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

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
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");
    gets(str);
    len=strlen(str);
    for(i=0;i<len;i++){

     int loc=0;  

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

      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];
    vowels++;
}     
  }
    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);
}
   getchar();

   return 0;
}

Recommended Answers

All 6 Replies

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

ass21

This post has no text-based content.

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).

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:

for(i=0;i<sizeof(vowstr);i++)
   vowstr[i]='\0';

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.