Complete these exercises to test your understanding of string arrays.
Write a program in C to input a string and print it.
C Code:
#include <stdio.h>
#include <stdlib.h>
void main()
{
char str[50];
printf("\n\nAccept a string from keyboard:\n");
printf("-----------------------------------\n");
printf("Input the string: ");
fgets(str, sizeof str, stdin);
printf("The string you entered is: %s\n", str);
}
-----------------------------------
Input the string: Welcome, w3resource
The string you entered is: Welcome, w3resource
#include <stdio.h>
#include <stdlib.h>
void main()
{
char str[100]; /* Declares a string of size 100 */
int l= 0;
printf("\n\nFind the length of a string:\n");
printf("---------------------------------\n");
printf("Input the string: ");
fgets(str, sizeof str, stdin);
while(str[l]!='\0')
{
l++;
}
printf("Length of the string is: %d\n\n", l-1);
}
#include <stdio.h>
#include <stdlib.h>
void main()
{
char str[100]; /* Declares a string of size 100 */
int l= 0;
printf("\n\nSeparate the individual characters from a string:\n");
printf("------------------------------------------------------\n");
printf("Input the string : ");
fgets(str, sizeof str, stdin);
printf("The characters of the string are: \n");
while(str[l]!='\0')
{
printf("%c ", str[l]);
l++;
}
printf("\n");
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char str[100]; /* Declares a string of size 100 */
int l,i;
printf("\n\nPrint individual characters of string in reverse order:\n");
printf("------------------------------------------------------\n");
printf("Input the string: ");
fgets(str, sizeof str, stdin);
l=strlen(str);
printf("The characters of the string in reverse are: \n");
for(i=l;i>=0;i--)
{
printf("%c ", str[i]);
}
printf("\n");
}
The program can also be written as below:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char str[100]; /* Declares a string of size 100 */
int l=0;
printf("\n\nPrint individual characters of string in reverse order:\n");
printf("------------------------------------------------------\n");
printf("Input the string: ");
fgets(str, sizeof str, stdin);
l=strlen(str);
printf("The characters of the string in reverse are: \n");
for(str[l]='\0';l>=0;l--)
{
printf("%c ", str[l]);
}
printf("\n");
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define str_size 100 //Declare the maximum size of the string
void main()
{
char str[str_size];
int i, wrd;
printf("\n\nCount the total number of words in a string:\n");
printf("------------------------------------------------------\n");
printf("Input the string: ");
fgets(str, sizeof str, stdin);
i = 0;
wrd = 1;
/* loop till end of string */
while(str[i]!='\0')
{
/* check whether the current character is white space or new line or tab character*/
if(str[i]==' ' || str[i]=='\n' || str[i]=='\t')
{
wrd++;
}
i++;
}
printf("Total number of words in the string is: %d\n", wrd-1);
}
// C program to compare the two strings
// without using strcmp() function
#include <stdio.h>
#define str_size 100 //Declare the maximum size of the string
int test(char* s1, char* s2)
{
int flag = 0;
while (*s1 != '\0' || *s2 != '\0') {
if (*s1 == *s2) {
s1++;
s2++;
}
else if ((*s1 == '\0' && *s2 != '\0')
|| (*s1 != '\0' && *s2 == '\0')
|| *s1 != *s2) {
flag = 1;
break;
}
}
return flag;
}
int main(void)
{
char str1[str_size], str2[str_size];
int flg=0;
printf("\nInput the 1st string: ");
fgets(str1, sizeof str1, stdin);
printf("Input the 2nd string: ");
fgets(str2, sizeof str2, stdin);
printf("\nString1: %s", str1);
printf("String2: %s", str2);
flg = test(str1, str2);
if(flg == 0)
{
printf("\nStrings are equal.\n");
}
else if(flg == 1)
{
printf("\nStrings are not equal.");
}
return 0;
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define str_size 100 //Declare the maximum size of the string
void main()
{
char str[str_size];
int alp, digit, splch, i;
alp = digit = splch = i = 0;
printf("\n\nCount total number of alphabets, digits and special characters :\n");
printf("--------------------------------------------------------------------\n");
printf("Input the string: ");
fgets(str, sizeof str, stdin);
/* Checks each character of string*/
while(str[i]!='\0')
{
if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
{
alp++;
}
else if(str[i]>='0' && str[i]<='9')
{
digit++;
}
else
{
splch++;
}
i++;
}
printf("Number of Alphabets in the string is: %d\n", alp);
printf("Number of Digits in the string is: %d\n", digit);
printf("Number of Special characters in the string is: %d\n\n", splch);
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void main()
{
char str1[100], str2[100];
int i;
printf("\n\nCopy one string into another string:\n");
printf("-----------------------------------------\n");
printf("Input the string: ");
fgets(str1, sizeof str1, stdin);
/* Copies string1 to string2 character by character */
i=0;
while(str1[i]!='\0')
{
str2[i] = str1[i];
i++;
}
//Makes sure that the string is NULL terminated
str2[i] = '\0';
printf("\nThe First string is : %s\n", str1);
printf("The Second string is: %s\n", str2);
printf("Number of characters copied: %d\n\n", i);
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define str_size 100 //Declare the maximum size of the string
void main()
{
char str[str_size];
int i, len, vowel, cons;
printf("\n\nCount total number of vowel or consonant:\n");
printf("----------------------------------------------\n");
printf("Input the string: ");
fgets(str, sizeof str, stdin);
vowel = 0;
cons = 0;
len = strlen(str);
for(i=0; i<len; i++)
{
if(str[i] =='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u' || str[i]=='A' || str[i]=='E' || str[i]=='I' || str[i]=='O' || str[i]=='U')
{
vowel++;
}
else if((str[i]>='a' && str[i]<='z') || (str[i]>='A' && str[i]<='Z'))
{
cons++;
}
}
printf("\nThe total number of vowel in the string is: %d\n", vowel);
printf("The total number of consonant in the string is: %d\n\n", cons);
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define str_size 100 //Declare the maximum size of the string
#define chr_no 255 //Maximum number of characters to be allowed
void main()
{
char str[str_size];
int ch_fre[chr_no];
int i = 0, max;
int ascii;
printf("\n\nFind maximum occurring character in a string:\n");
printf("--------------------------------------------------\n");
printf("Input the string : ");
fgets(str, sizeof str, stdin);
for(i=0; i<chr_no; i++) //Set frequency of all characters to 0
{
ch_fre[i] = 0;
}
/* Read for frequency of each characters */
i=0;
while(str[i] != '\0')
{
ascii = (int)str[i];
ch_fre[ascii] += 1;
i++;
}
max = 0;
for(i=0; i<chr_no; i++)
{
if(i!=32)
{
if(ch_fre[i] > ch_fre[max])
max = i;
}
}
printf("The Highest frequency of character '%c' appears number of times: %d \n\n", max, ch_fre[max]);
}
#include <stdio.h>
#include <string.h>
void main()
{
char str[100],ch;
int i,j,l;
printf("\n\nSort a string array in ascending order:\n");
printf("--------------------------------------------\n");
printf("Input the string : ");
fgets(str, sizeof str, stdin);
l=strlen(str);
/* sorting process */
for(i=1;i<l;i++)
for(j=0;j<l-i;j++)
if(str[j]>str[j+1])
{
ch=str[j];
str[j] = str[j+1];
str[j+1]=ch;
}
printf("After sorting the string appears like: \n");
printf("%s\n\n",str);
}
#include <stdio.h>
#include <string.h>
void main()
{
char name[25][50],temp[25];
int n,i,j;
printf("\n\nSorts the strings of an array using bubble sort :\n");
printf("-----------------------------------------------------\n");
printf("Input number of strings :");
scanf("%d",&n);
printf("Input string %d :\n",n);
for(i=0;i<=n;i++)
{
fgets(name[i], sizeof name, stdin);
}
/*Logic Bubble Sort*/
for(i=1;i<=n;i++)
for(j=0;j<=n-i;j++)
if(strcmp(name[j],name[j+1])>0)
{
strcpy(temp,name[j]);
strcpy(name[j],name[j+1]);
strcpy(name[j+1],temp);
}
printf("The strings appears after sorting:\n");
for(i=0;i<=n;i++)
printf("%s\n",name[i]);
}
#include <stdio.h>
void main()
{
char str[100], sstr[100];
int pos, l, c = 0;
printf("\n\nExtract a substring from a given string:\n");
printf("--------------------------------------------\n");
printf("Input the string: ");
fgets(str, sizeof str, stdin);
printf("Input the position to start extraction:");
scanf("%d", &pos);
printf("Input the length of substring:");
scanf("%d", &l);
while (c < l)
{
sstr[c] = str[pos+c-1];
c++;
}
sstr[c] = '\0';
printf("The substring retrieve from the string is: \" %s\ "\n\n", sstr);
}
#include <stdio.h>
void main()
{
char str[80],search[20];
int c1=0,c2=0,i,j,flg;
printf("\n\nCheck whether a given substring is present in the given string :\n");
printf("-------------------------------------------------------------------\n");
printf("Input the string: ");
fgets(str, sizeof str, stdin);
printf("Input the substring to be search: ");
fgets(search, sizeof search, stdin);
while (str[c1]!='\0')
c1++;
c1--;
while (search[c2]!='\0')
c2++;
c2--;
for(i=0;i<=c1-c2;i++)
{
for(j=i;j<i+c2;j++)
{
flg=1;
if (str[j]!=search[j-i])
{
flg=0;
break;
}
}
if (flg==1)
break;
}
if (flg==1)
printf("The substring exists in the string.\n\n");
else
printf("The substring is not exists in the string. \n\n");
}
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void main()
{
char str[100];
int ctr, ch, i;
printf("\n\nReplace lowercase characters by uppercase and vice-versa:\n");
printf("--------------------------------------------------------------\n");
printf("Input the string: ");
fgets(str, sizeof str, stdin);
i=strlen(str);
ctr = i; /*shows the number of chars accepted in a sentence*/
printf("\nThe given sentence is: %s",str);
printf("After Case changed the string is: ");
for(i=0; i < ctr; i++)
{
ch = islower(str[i]) ? toupper(str[i]) : tolower(str[i]);
putchar(ch);
}
printf("\n\n");
}
#include <stdio.h>
#include <string.h>
void main()
{
int ctr=0,i,freq=0;
int t,h,e,spc;
char str[100];
printf("\n\nFind the number of times the word 'the ' in any combination appears:\n");
printf("----------------------------------------------------------------------\n");
printf("Input the string: ");
fgets(str,sizeof str,stdin);
ctr=strlen(str);
/*Counts the frequency of the word 'the' with a trailing space*/
for(i=0;i<=ctr-3;i++)
{
t=(str[i]=='t'||str[i]=='T');
h=(str[i+1]=='h'||str[i+1]=='H');
e=(str[i+2]=='e'||str[i+2]=='E');
spc=(str[i+3]==' '||str[i+3]=='\0');
if ((t&&h&&e&&spc)==1)
freq++;
}
printf("The frequency of the word \'the\' is: %d\n\n",freq);
}
#include <stdio.h>
#include <string.h>
void main(){
char str[150];
int i,j;
printf("\n\nRemove characters in String Except Alphabets:\n");
printf("--------------------------------------------------\n");
printf("Input the string : ");
fgets(str,sizeof str,stdin);
for(i=0; str[i]!='\0'; ++i)
{
while (!((str[i]>='a'&&str[i]<='z') || (str[i]>='A'&&str[i]<='Z' || str[i]=='\0')))
{
for(j=i;str[j]!='\0';++j)
{
str[j]=str[j+1];
}
str[j]='\0';
}
}
printf("After removing the Output String: %s\n\n",str);
}
#include <stdio.h>
void main(){
char str[1000],choice;
int i,ctr=0;
printf("\n\nFind the Frequency of Characters:\n");
printf("--------------------------------------\n");
printf("Input the string : ");
fgets(str,sizeof str,stdin);
printf("Input the character to find frequency: ");
scanf("%c",&choice);
for(i=0;str[i]!='\0';++i)
{
if(choice==str[i])
++ctr;
}
printf("The frequency of '%c' is: %d\n\n", choice, ctr);
}
#include <stdio.h>
#include <string.h>
void main()
{
char str1[100], str2[100], i, j,l,m,k;
printf("\n\nConcatenate Two Strings Manually:\n");
printf("-------------------------------------\n");
printf("Input the first string: ");
fgets(str1,sizeof str1,stdin);
printf("Input the second string: ");
fgets(str2,sizeof str2,stdin);
l=strlen(str1);
m=strlen(str2);
for(i=0; i<l-1; ++i); /* value i contains reaches the end of string str1. */
str1[i]=' '; /* add a space with string str1. */
i++; /* value i increase by 1 for the blank space */
for(j=0; j<m-1; ++j, ++i)
{
str1[i]=str2[j];
}
k=strlen(str1);
printf("After concatenation the string is: \n ");
for(i=0; i<k; ++i)
{
printf("%c",str1[i]);
}
printf("\n\n");
}
Concatenate Two Strings Manually:
-------------------------------------
Input the first string: this is string one
Input the second string: this is string two
After concatenation the string is:
this is string one this is string two
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void main()
{
char str[100], word[20], mx[20], mn[20], c;
int i = 0, j = 0, flg = 0;
printf("\n\nFind the largest and smallest word in a string:\n");
printf("-----------------------------------------------------\n");
printf("Input the string : ");
i = 0;
do
{
fflush(stdin);
c = getchar();
str[i++] = c;
} while (c != '\n');
str[i - 1] = '\0';
for (i = 0; i < strlen(str); i++)
{
while (i < strlen(str) && !isspace(str[i]) && isalnum(str[i]))
{
word[j++] = str[i++];
}
if (j != 0)
{
word[j] = '\0';
if (!flg)
{
flg = !flg;
strcpy(mx, word);
strcpy(mn, word);
}
if (strlen(word) > strlen(mx))
{
strcpy(mx, word);
}
if (strlen(word) < strlen(mn))
{
strcpy(mn, word);
}
j = 0;
}
}
printf("The largest word is '%s' \nand the smallest word is '%s' \nin the string: '%s'.\n", mx, mn, str);
}
#include<stdio.h>
#include<ctype.h>
int main()
{
int ctr=0;
char str_char;
char str[100];
printf("\n Convert a string to uppercase:\n");
printf("-----------------------------------");
printf("\n Input a string in lowercase: ");
fgets(str, sizeof str, stdin);
printf(" Here is the above string in UPPERCASE:\n ");
while (str[ctr])
{
str_char=str[ctr];
putchar (toupper(str_char));
ctr++;
}
printf("\n\n");
return 0;
}
Convert a string to uppercase:
-----------------------------------
Input a string in lowercase: the quick brown fox jumps over the lazy dog.
Here is the above string in UPPERCASE:
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG.
#include<stdio.h>
#include<ctype.h>
int main()
{
int ctr=0;
char str_char;
char str[100];
printf("\n Convert a string to lowercase:\n");
printf("----------------------------------");
printf("\n Input a string in UPPERCASE: ");
fgets(str, sizeof str, stdin);
printf(" Here is the above string in lowercase:\n ");
while (str[ctr])
{
str_char=str[ctr];
putchar (tolower(str_char));
ctr++;
}
return 0;
}
#include<stdio.h>
#include<ctype.h>
int main()
{
char TestChar;
printf("\n Check whether a character is Hexadecimal Digit or not:\n");
printf("-------------------------------------------------------\n");
printf(" Input a character: "); // Hexadecimal Digits are a-f, A-F or 0-9
scanf( "%c", &TestChar );
if( isxdigit(TestChar) )
printf( " The entered character is a hexadecimal digit. \n" );
else
printf( " The entered character is not a hexadecimal digit. \n" );
return 0;
}
#include<stdio.h>
#include<ctype.h>
int main()
{
char TestChar;
printf("\n Check whether a letter is uppercase or not:\n");
printf("------------------------------------------------\n");
printf(" Input a character: ");
scanf( "%c", &TestChar );
if( isupper(TestChar) )
printf( " The entered letter is an UPPERCASE letter. \n" );
else
printf( " The entered letter is not an UPPERCASE letter. \n" );
return 0;
}
#include<stdio.h>
#include<ctype.h>
int main()
{
int new_char;
char t;
int ctr=0;
char str[100];
printf("\n Replace the spaces of a string with a specific character:\n");
printf("-------------------------------------------------------------\n");
printf(" Input a string: ");
fgets(str, sizeof str, stdin);
printf(" Input replace character: ");
scanf("%c",&t);
printf(" After replacing the space with %c the new string is:\n",t);
while (str[ctr])
{
new_char=str[ctr];
if (isspace(new_char))
new_char=t;
putchar (new_char);
ctr++;
}
printf("\n\n");
return 0;
}
#include<stdio.h>
#include<ctype.h>
int main()
{
int ctr1=0;
int ctr2=0;
char str[100];
printf("\n Count the number of punctuation characters exists in a string:\n");
printf("------------------------------------------------------------------\n");
printf(" Input a string: ");
fgets(str, sizeof str, stdin);
while (str[ctr1])
{
if (ispunct(str[ctr1])) ctr2++;
ctr1++;
}
printf (" The punctuation characters exists in the string is: %d\n\n", ctr2);
return 0;
}
#include<stdio.h>
#include<ctype.h>
int main()
{
int ctr=0;
char str[]=" The quick brown fox \n jumps over the \n lazy dog. \n";
printf("\n Print only the string before new line character:\n");
printf("----------------------------------------------------\n");
while (isprint(str[ctr]))
{
putchar (str[ctr]);
ctr++;
}
printf("\n\n");
return 0;
}
#include<stdio.h>
#include<ctype.h>
int main()
{
char TestChar;
printf("\n Check whether a letter is lowercase or not:\n");
printf("------------------------------------------------\n");
printf(" Input a character: ");
scanf( "%c", &TestChar );
if( islower(TestChar) )
printf( " The entered letter is a lowercase letter. \n" );
else
printf( " The entered letter is not a lowercase letter. \n" );
return 0;
}
#include<stdio.h>
#include<ctype.h>
int main()
{
FILE * pfile;
int a;
printf("\n Remove the spaces between two words:\n");
printf("-----------------------------------------\n");
// file.txt contain : the quick brown fox jumps over the lazy dog
pfile=fopen ("file.txt","r");
printf(" The content of the file is :\n The quick brown fox jumps over the lazy dog\n\n");
printf(" After removing the spaces the content is: \n");
if (pfile)
{
do {
a = fgetc (pfile);
if ( isgraph(a) ) putchar (a);
} while (a != EOF);
fclose (pfile);
}
printf("\n\n");
return 0;
}
#include<stdio.h>
#include<ctype.h>
int main()
{
char TestChar;
printf("\n Check whether a character is digit or not:\n");
printf("----------------------------------------------\n");
printf(" Input a character: ");
scanf( "%c", &TestChar );
if( isdigit(TestChar) )
printf( " The entered character is a digit. \n\n" );
else
printf( " The entered character is not a digit. \n\n" );
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char str1[100];
char newString[10][10];
int i,j,ctr;
printf("\n\n Split string by space into words:\n");
printf("---------------------------------------\n");
printf(" Input a string: ");
fgets(str1, sizeof str1, stdin);
j=0; ctr=0;
for(i=0;i<=(strlen(str1));i++)
{
// if space or NULL found, assign NULL into newString[ctr]
if(str1[i]==' '||str1[i]=='\0')
{
newString[ctr][j]='\0';
ctr++; //for next word
j=0; //for next word, init index to 0
}
else
{
newString[ctr][j]=str1[i];
j++;
}
}
printf("\n Strings or words after split by space are:\n");
for(i=0;i < ctr;i++)
printf(" %s\n",newString[i]);
return 0;
}
#include<stdio.h>
#include
int ifexists(char p, char q[], int v)
{
int i;
for (i=0; i<v;i++)
if (q[i]==p) return (1);
return (0);
}
int main()
{
char string1[80],string2[80];
int n,i,x;
printf("Input a string: ");
scanf("%s",string1);
n=strlen(string1);
string2[0]=string1[0];
x=1;
for(i=1;i < n; i++)
{
if(ifexists(string1[i], string2, x))
{
printf("The first repetitive character in %s is: %c ", string1, string1[i]);
break;
}
else
{
string2[x]=string1[i];
x++;
}
}
if(i==n)
printf("There is no repetitive character in the string %s.", string1);
}
#include<stdio.h>
#include<string.h>
int if_char_exists(char c, char p[], int x, int y[])
{
int i;
for (i=0; i<=x;i++)
{
if (p[i]==c)
{
y[i]++;
return (1);
}
}
if(i>x) return (0);
}
int main()
{
char str1[80],chr[80];
int n,i,x,ctr[80];
printf("Enter a str1ing: ");
scanf("%s",str1);
n=strlen(str1);
chr[0]=str1[0];
ctr[0]=1;
x=0;
for(i=1;i < n; i++)
{
if(!if_char_exists(str1[i], chr, x, ctr))
{
x++;
chr[x]=str1[i];
ctr[x]=1;
}
}
printf("The count of each character in the string %s is \n", str1);
for (i=0;i<=x;i++)
printf("%c\t%d\n",chr[i],ctr[i]);
}
#include
int main()
{
char string1[255];
int i;
printf("Input a sentence: ");
gets(string1);
printf("The original string:\n");
puts(string1);
i=0;
while(string1[i]!='\0')
{
if(string1[i]=='a' ||string1[i]=='e' ||string1[i]=='i' ||string1[i]=='o' ||string1[i]=='u')
string1[i]=string1[i]-32;
i++;
}
printf("After converting vowels into upper case the sentence becomes:\n");
puts(string1);
}