C Program - to accept 'n' strings from user and store it in a char array

I am currently using Turbo C++ Version 3.0 (one with blue screen)

Can any one help with the C Code of the subject ?

Recommended Answers

All 10 Replies

Yes, the vast majority of us can help. But we won't do it for you.

Because Daniweb has rules. Specifically: "Do provide evidence of having done some work yourself if posting questions from school or work assignments"

Your problem is clearly a basic exercise intended for learning, and thus constitutes homework. Therefore we expect a certain measure of effort on your part before offering help. If we did the work for you, you'd learn nothing.

i am trying this code - but it shows the count zero

can you help

    #include<stdio.h>
    #include<conio.h>

    void main(){
    int i,j;
    int count=0;
    char z[10][100];
    clrscr();
    for(i=0;i<3;i++)
    {
    printf("\nEnter a string: ");
    //scanf("%[^\t\n]s",z[i]);
    gets(z[i]);
    }
    for(i=0;i<10;i++){
    for(j=0;j<10;j++){
    if(z[i]=='a'){count++;}
    }
    }


    printf("%d",count);
    getch();


}

You're testing z[i], which is an array rather than a string. Try this instead:

for (i = 0; i < 3; i++)
{
    for (j = 0; z[i][j] != '\0'; j++)
    {
        if (z[i][j] == 'a')
        {
            ++count;
        }
    }
}

Note that the outer loop only goes through the number of strings you actually input, this is important if you don't initialize all of the strings to be blank. The inner loop only searches the contents of the string (which ends with a '\0' character in a valid string). Finally, the condition checks the characters in each string as you're using an array of arrays of char.

i tried this but still the count shows 0
i want the o/p array to be sorted in ascending order of vowels each of the string contain--Please help

#include<stdio.h>
#include<conio.h>

void main(){
int i,j;
int count;
char z[10][100];
clrscr();
for(i=0;i<3;i++)
{
printf("\nEnter a string: ");
//scanf("%[^\t\n]s",z[i]);
gets(z[i]);
}
for(i=0;i<10;i++){

for(j=0;j<100;j++){
if(z[i][j]=='a'){count++;}
}
}


printf("%d",count);
getch();

}

line 17 is wrong. If you enter the string "Hello" why would you check all 100 bytes of the array when only the first 5 bytes are used?

for(i = 0; i < 10; i++)
{
   int len = strlen(z[i]);
   for(j = 0; j < len; j++)

i want the o/p array to be sorted in ascending order of vowels each of the string contain

I don't get it. If the strings are
"Hello"
"World"
In which order should those two strings appear?

int len = strlen(z[i]);
for(j = 0; j < len; j++)

All strlen does is search for a '\0' character, so it's more efficient to simply do that yourself as in my example in a previous post. This is assuming the string is valid in the first, place, of course. ;)

Yes, I agree.

try this...

#include <stdio.h>

void main()
{
    char string[20];
    int n, count = 0;
    printf("Enter the no of characters present in an array \n ");
    scanf("%d", &n);
    printf(" Enter the string of %d characters \n" , n);
    scanf("%s", string);
    while (count < n)
    {
        printf(" %c = %d\n", string[count], string[count] );
        ++ count ;
    }
}
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.