본문 바로가기
학부공부/C_Basic

7. String[C Basic]

by sonpang 2021. 11. 7.
반응형

* example

#include <stdio.h>
#include <string.h>
int checkcase(char cha);
void convertCharCase(char *sPtr);
int main(void) {
   char str[200] = "PoINteR is vERy ImPorTAnt in C pRoGraMMing, SO STudEnt nEEd tO pRACtiCe usING poINTer";


   convertCharCase(str);
}
int checkcase(char cha) {
   if (65 <= cha && cha <= 90) {
   		printf("1"); 
     	return 1;
   }
   if (97 <= cha && cha <= 122) {
   		printf("0"); 
    	return 0;
   }

}
void convertCharCase(char *sPtr) {
   int a = strlen(sPtr);
   int i, j;

   for (i = 0; i < a; i++) {
      if (checkcase(sPtr[i]) == 1) {
        	sPtr[i] += 32;
      }
      if (checkcase(sPtr[i]) == 0) {
        	sPtr[i] -= 32;
      }
   }
   for (j = 0; j < a; j++) {
      printf("%c", sPtr[j]);
   }
}

 

7.1.

Write a program that inputs four strings that represent integers. 

Converts the strings to integers, sums the values and prints the total of the four values. 

Use “atoi” function in “stdlib” library.

#include<stdio.h>
#include<stdlib.h>
int main()
{
	char str[4][10];
	int i, sum=0;
	for (i = 0;i < 4;i++) {
		printf("Enter an ainteger string : ");
		scanf("%s", &str[i]);
		sum += atoi(str[i]);
	}
	printf("\nThe total of the values is %d", sum);

	return 0;
}

 

7.2.

Write a program that inputs 3 lines of text and uses function “strchr” to determine the total occurences of each letter of the alphabet in the lines of text. 

Uppercase and lowercase letters should be counted together. 

Store the totals for each letter in an array and prints the values.

#include<stdio.h>
#include<string.h>
int main()
{
	char str[3][80];
	char *search;
	int i, j, count[26] = {0};
	
	printf("Enter three lines of text : \n");
	for(i=0;i<3;i++){
		gets(str[i]);
		for(j=0;str[i][j];j++)
			str[i][j]=tolower(str[i][j]);
	}
	for(i=0;i<26;i++){
		for(j=0;j<3;j++){
			search=str[j];
			while(search=strchr(search,'a'+i)){
				count[i]++;
				*search++;
			}
		}
	}
	
	printf("\nThe total occurrences of each character : \n");
	for (i = 0;i < 26;i++)
		printf("%c: %3d\n", i + 97, count[i]);

	return 0;
}

 

7.3.

Write a program that reads three lines of text and prints a table indicating the number of one-letter words, two-letter words, threeletter words, and so on. 

Use “strtok” and “strlen” functions in “string” library.

#include<stdio.h>
#include<string.h>
int main() {
	char str[3][80], *temp;
	int i, count[20]={0};
	printf("Enter three lines of text :\n");
	for (i = 0;i < 3;i++) {
		gets(str[i]);
		temp=strtok(str[i]," .\n");
		while(temp){
			count[strlen(temp)]++;
			temp = strtok(NULL, " .\n");
		}
	}

	for(i=0;i<20;i++){
		if(count[i])
			printf("\n%d %s of length %d",count[i],count[i]==1?"word":"words",i);
	}
	
	return 0;
}

 

7.4.

Write a program that reads 10 strings, alphabetizes and prints the list of strings. 

Use bubblesort alrgorithm (increasing order) 

Use “strcpy” and “strcmp” functions in “string” library.

#include<stdio.h>
#include<string.h>
int main()
{
	int i,j,k;
	char text[10][20], temp[20];
	for(i=0;i<10;i++){
		printf("Enter a string : ");
		gets(text[i]);
	}
	for(i=0;i<9;i++){
		for(j=i+1;j<10;j++){
			if(strcmp(text[i],text[j])==1){
				strcpy(temp,text[i]);
				strcpy(text[i],text[j]);
				strcpy(text[j],temp);
			}
		}
		for(i=0;i<10;i++)
			printf("%s ",text[i]);
	}
	printf("\nThe strings in sorted order are :\n");
	for(i=0;i<10;i++)
		printf("%s\n",text[i]);
	
	return 0;
}

 

7.5.

Wirte a program that read 3 lines of text and prints a table indicating the number of occurrences of each different word in the text.

#include<stdio.h>
#include<string.h>
int check(char word[][20],char *temp){
	int i;
	for(i=1;i<30;i++){
		if(!strcmp(word[i],temp))
			return i;
	}
	return 0;
}
int main()
{
	int i,j=1,index, count[30]={0};
	char text[3][80], word[30][20]={0}, *temp;
	
	printf("Enter three lines of text :\n");
	for(i=0;i<3;i++){
		gets(text[i]);
		temp=strtok(text[i]," .\n");
		while(temp){
			if(index=check(word,temp))
				count[index]++;
			else{
				count[j]++;
				strcpy(word[j++],temp);
			}
			temp = strtok(NULL, " .\n");
		}
	}
	
	printf("\n");
	for(i=1;count[i];i++){
		printf("\"%s\" appeared %d time\n",word[i],count[i]);
	}
	
	return 0;
}
반응형

'학부공부 > C_Basic' 카테고리의 다른 글

9. Struct[C Basic]  (0) 2021.11.07
8. Bit operator[C Basic]  (0) 2021.11.07
6. Pointer[C Basic]  (0) 2021.11.07
5. Array and Functions[C Basic]  (0) 2021.11.07
4. Function[C Basic]  (0) 2021.11.07

댓글