prob3-w3

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.2 kB
1
Indexable
Never
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(){
	char str[100];
	char words[10][10];
	char finalStr[100];
	int i = 0, j = 0, k = 0;
	
	// Input string 
	printf("Enter your string: "); 
	gets(str);
	printf("The original string: %s\n", str);


	// Print words and remove redundant spaces between words
	if (str[0] == ' '){
	} else {
		words[j][k] = str[0];
		k++; 
	}
	for (int i = 1; i <= strlen(str); i++){
		if ((str[i] == ' ' || str[i] == '\0') && str[i - 1] == ' '){
		} else if (str[i] == ' ' || str[i] == '\0'){
			words[j][k] = '\0';
			j++; 
			k = 0;
		} else {
			words[j][k] = str[i];
			k++;
		}
	}
	for (i = 0; i < j; i++){
		printf("%s\n", words[i]);
	}

	// print out the final string in proper form
	i = 0;
	k = 0;
	for (int t = 0; t < j; t++){
		for (int h = 0; h < 10; h++){
			if (words[t][h] == '\0'){
				finalStr[i] = ' ';
				i++;  
				break;
			} else {
				finalStr[i] = words[t][h];
			}
			i++;
		}
	}
	// Upper the first letter in the string
	finalStr[0] = toupper(finalStr[0]);
	printf("Final string (proper form): %s", finalStr);

	return 0;
}