I want to print each first character of a List of Array in upper case using C.

For example, if I have Array of these elements

char *d[] = {"water", "salt", "sugar", "milk", "soda"};

I want my function to print them like this

Water Salt Sugar Milk Soda

I will appreciate any help.


Thank

Dani AI

Generated

Good direction from to "start simple" and a solid attempt from — the main practical gap is how to get the number of elements without hard‑coding it, plus a couple of safety notes about toupper. Two common approaches are (A) compute the element count at the point the array is declared using sizeof, or (B) use a sentinel NULL so the caller does not need to pass a length. The example below shows approach (A) and a safe way to uppercase just the first character of each string.

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

int main(void) {
    char *words[] = { "apple", "banana", "cherry", "date" };
    size_t n = sizeof words / sizeof words[0];

    for (size_t i = 0; i < n; ++i) {
        char *s = words[i];
        if (s && *s) {
            unsigned char c = (unsigned char) s[0];   /* avoid UB for negative char */
            putchar(toupper(c));
            fputs(s + 1, stdout);
        }
        if (i + 1 < n) putchar(' ');
    }
    putchar('\n');
    return 0;
}

Notes and cautions:

  • sizeof only works where the array is actually defined; once passed to a function it decays to a pointer. Either compute the count in the caller and pass it, or terminate the array with NULL and loop until that sentinel.
  • toupper takes and returns int; pass an unsigned char cast to avoid undefined behavior on platforms where char is signed.
  • Handle NULL pointers and empty strings to avoid crashes.
  • For non‑ASCII or locale‑sensitive capitalization, consider wide chars or locale-aware APIs.

Recommended Answers

All 3 Replies

Start by writing some code that outputs each word.

Then change the code to add capitalization.

1

[edit] If you're going to remove a message, leave a useful message in it's place. Like "Duplicate removed". [/edit]

bellow i tried to solve your problem bt the problem i m facing that i cant count the number of words in the array ..so i passed the number manually in line 10..if you can solve it pls inform

//Pirate™
#include<stdio.h>
#include<stdlib.h>

void print_words(char *str[],int m);

void main()
{
        char *d[]={"water","salt","suger","milk","soda"};
	print_words(d,5);
}

void print_words(char *str[],int m)
{
	char *temp;
	
for(int i=0;i<m;i++)
	{
		int j=0;
	
		temp=str[i];

		while(*temp)
		{
			if(j==0)
				printf("%c",toupper(*temp));
			else
				printf("%c",*temp);
			j++;
			temp++;
			
		}
		printf(" ");
		
	}
	
}
commented: Do NOT try to solve problems for people here. Help them solve the problems themselves -2
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.