2

In this code im trying to take a string from user and put the uppercase letters into a other array then print it. But im doing something horribly wrong i can tell, can someone help me with this code?

#include<string.h>

int main() {
   char arr[100],uppercaseletters[50];
   printf("Enter array:");
   scanf("%s",arr);
   strlen(arr);
   printf("The lenght of string is: %d\n",strlen(arr));
   for(int i=0;i<=strlen(arr);i++){
    if(arr[i]>='A' && arr[i]<='Z'){
        arr[i]+=uppercaseletters[50];
        printf("%s",uppercaseletters);
       }





   }

}

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
gorcc
  • 35
  • 3

3 Answers3

2

For starters according to the C Standard the function main without parameters shall be declared like

int main( void )

It is unclear why the array uppercaseletters has less elements than the array arr.

char arr[100],uppercaseletters[50];

The user can enter a string consisting only from upper case characters.

This statement

strlen(arr);

does not have an effect.

This statement

arr[i]+=uppercaseletters[50];

does not make sense. You have to fill the array uppercaseletters. Moreover the element uppercaseletters[50] does not exist because the valid range of indices is [0, 50).

In this statement

printf("%s",uppercaseletters);

you are trying to output a non-initialized array.

The program can look the following way.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(void) 
{
    enum { N = 100 };
    char s[N];

    printf( "Enter a string: " );

    fgets( s, sizeof( s ), stdin );

    size_t n = 0;

    for ( size_t i = 0; s[i] != '\0'; i++ )
    {
        if ( isupper( ( unsigned char )s[i] ) ) ++n;
    }

    char *upper_case_letters = malloc( n + 1 );

    n = 0;

    for ( size_t i = 0; s[i] != '\0'; i++ )
    {
        if ( isupper( ( unsigned char )s[i] ) )
        {
            upper_case_letters[n++] = s[i];
        }
    }

    upper_case_letters[n] = '\0';

    puts( upper_case_letters );

    free( upper_case_letters );

    return 0;
}

The program output might look for example like

Enter a string: Hello World!
HW

Without dynamically allocating an array for upper case letters the program can look the following way.

#include <stdio.h>
#include <ctype.h>

int main(void) 
{
    enum { N = 100 };
    char s[N];
    char upper_case_letters[N];

    printf( "Enter a string: " );

    fgets( s, sizeof( s ), stdin );

    size_t n = 0;

    for ( size_t i = 0; s[i] != '\0'; i++ )
    {
        if ( isupper( ( unsigned char )s[i] ) )
        {
            upper_case_letters[n++] = s[i];
        }
    }

    upper_case_letters[n] = '\0';

    puts( upper_case_letters );

    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • I don't think there is a need for `malloc` here, you could have declared `char upper_case_letters[N]`. Other than that, nice explanation. – Pablo Jan 22 '20 at 01:48
1

arr[i]+=uppercaseletters[50];

This is wrong. You want to add the uppercase letters to a new array, then use a separate index, such as j:

int j = 0;
int len = strlen(arr);
for (int i = 0; i < len-1; i++)
{
    if (arr[i] >= 'A' && arr[i] <= 'Z')
    {
        // arr[i]+=uppercaseletters[50];
        if(j < 49)
        {
            uppercaseletters[j++] = arr[i];
        }
    }
}
uppercaseletters[j] = '\0';
printf("%s", uppercaseletters);

You also need to put the printf outside the loop as above - once the array has been populated.

artm
  • 17,291
  • 6
  • 38
  • 54
0

We will get a series from the user, we will throw the A-Z capital letters to another series and print it on the screen.

In fact, its logic is very simple. While navigating the items in the array with the 'for' loop, if A-Z detects letters, it assigns it to the new array.

1) Let's add our Standard Input / Output library and add our initial function:

#include <stdio.h>
int main()
{

2) Create the arrays and variables:

char arr[100], uppercaseletters[50];
int i=0, j=0;
//We created the variables 'i' and 'j' to navigate the characters of our series.

3) Let's get the array from the user:

printf("Enter array: ");
scanf("%s",&arr);

4) Let's create our loop, first return 'i = 0' and the number of items in the array.

The number of items in the array can be found with -strlen or -arr [i]! = '\ 0'.

for(i=0;arr[i]!='\0';i++)
{

5) Let's add the condition to the loop. If the 'i' element is greater than A and less than Z, add it to the other array.

if(arr[i]>='A'&& arr[i]<='Z')
{
    uppercaseletters[j++] = arr[i];
    //The reason we do 'j++' is to add to the 0th element of the array at first and it will be 'j = 1', then it will add to the 1st element and it will continue as 'j = 2'.
}
}

6) Let's print our upper case array on the screen. Also, the number of items in our array was 'j'.

for(i=0;i<j;i++)
{
    printf("\n%c",uppercaseletters[i]);
}
//FİNİSH
return 0;
}

7) I hope this helps ...

#include <stdio.h>
int main()
{
    char arr[100], uppercaseletters[50];
    int i=0, j=0;
    //We created the variables 'i' and 'j' to navigate the characters of our series.

    printf("Enter array: ");
    scanf("%s",&arr);

    for(i=0;arr[i]!='\0';i++)
    {
        if(arr[i]>='A'&& arr[i]<='Z')
        {
            uppercaseletters[j++] = arr[i];
            //The reason we do 'j++' is to add to the 0th element of the array at first and it will be 'j = 1', then it will add to the 1st element and it will continue as 'j = 2'.
        }
    }

    for(i=0;i<j;i++)
    {
        printf("\n%c",uppercaseletters[i]);
    }
    //FİNİSH
    return 0;
}

My English is not so good :)

Hamza
  • 1
  • 1