C: Strings - Applications resolved


1) Accomplish a program that shows the difference between 'A' and 'A'.
#include<stdio.h>
#include<conio.h>
void main()
{
  char a1='A';
  char a2[]="A";
  printf("%c %c %s",a1,a2[0],a2); 	  //display the contents of   two variables
  printf("\n%d %d",sizeof(a1),sizeof(a2)); //check memory usage of the two variables
  printf("\n%s ie %d",a2[1],a2[1]); 	//see which is the second element
  getch();
}
2) Accomplish a program that displays a string uppercase alphabet and do processing on it.
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
	char a[256];
	for(int i=0;i<26;i++)
		a[i]='A'+i;
	
	puts("String contains:");
	printf("\"%s\"",a);
 
	//want at the end to add character '1 'and redisplay the string
	a[26]='1';
	a[27]='2';
	a[28]=0;
	printf("\n\"%s\"",a);
 
	//display the number of characters of the string
	printf("\nNumber of characters is %d",strlen(a));
 
	//displays the string starting with the second element until the tenth
	a[10]=0; //or NULL
	printf("\n\"%s\"",a+1); // !!! do not put a++
	printf("\n\"%s\"",a+6);
 
	//redisplay the number of characters of the string
	printf("\nNumber of characters is %d",strlen(a));
 
	/*introduce a new variable p will hold the address of the array of characters (a) 
	is itself an address and the address is the same as the first element, ie a[0]*/
	char *p;
	p=a; /*this assignment is correct, the variable p retaining character vector address a, 
	ie the address of its first byte. Instead, the award is improper for a = p. it is a constant */
 
	//After allocation we use p in the same conditions as the a
	printf("\n%s",p);
	//four equivalent relationships display vector address
	printf("\n%p",p);
	printf("\n%p",a);
	printf("\n%p",&p[0]);
	printf("\n%p",&a[0]);
 
	/* if the content of variable p is added 1, it will retain the vector address
 	whose first element coincides with the second byte (element) vector*/
	printf("\n%s",++p); //p++; printf("\n%s",p);
	printf("\n%s",p++);
 
	//two equivalent addresses
	printf("\n%p",p);
	printf("\n%p",&a[2]);
 
	//new vector can address and byte
	printf("\n%c",p[0]); //display element 'C'
	printf("\n%c",p[-2]); //display element 'A'

	/* decreases can be made between addresses; In this case the result is a whole 
	and the "p-a" to get the index of the first byte in the note in p */
	printf("\n%d",p-a);
 
	getch();
}
3) The difference between the type string of elements representing characters and address type to store a string.
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
	char a[]="Hello C!"; // string of elements that represent characters
	char* b="Hello C!"; // address type for storing a strin
	puts(a);
	puts(b);
	printf("%d",sizeof(a));
	printf("\n%d",sizeof(b));
	getch();
}
4) It reads a string from the keyboard; display character by character and determine the number of characters and giving it to another string to add or to copy in the first string.
#include<string.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
	char* a; //string entered by user
	int i; //string index

	a=(char*)malloc(sizeof(char)*255) ;
 
	printf("Enter a string and press Enter\n");
	gets(a); //scanf("%s",&a);
 
	//Display each character until it finds the NULL
	for(i=0;a[i]!=NULL;i++)
	putchar(a[i]);
	//or printf("%c",a[i]);
	printf("\nNumber of characters is %d ie %d",i,strlen(a)); //not include NULL character
 
	//I want to enter a new string
	char b[]="another string";
	strcat(a,b); //add address string "b" in the string "a" (I realize the concatenation of two)
	printf("\nThe new string a is \"%s\"",a); // or puts("The new string a is "); puts(a);
	strcpy(b,a); //copies the string "a" to "b"
	puts("\nThe new string b is:");
	puts(b);
 
	//compare two strings
	printf("%d",strcmp(a,b));
 
	getch();
}
5) Determine the number of words in a text.
#include<stdio.h>
#include<string.h>
#include<conio.h>
void main()
{
	int k=0;
	char txt[1024]; //string entered by user
	char* p;
 	printf("Enter a character string and press Enter\n");
	gets(txt); //scanf("%s",&txt);

	p=strtok(txt," ");
		while (p!=NULL)
	{
		p=strtok(NULL, " ");
		k++;
	} 
	printf("\nNumber of words is: %d",k); //not include NULL character

	getch();
}
6) Determine the number of words in a text using its own function.
#include<stdio.h>
#include<string.h>
#include<conio.h>

int separation(char* txt, char* p)
{
	int k=0;
	p=strtok(txt," ");	
	while (p!=NULL)
	{
		p=strtok(NULL, " ");
		k++;
	}
	return k;
}
void main()
{
	int k;
	char txt[1024]; //string entered by user
	char p; 
	printf("Enter a character string and press Enter\n");
	gets(txt); //scanf("%s",&txt);

	printf("\nNumber of words is: %d",separation(txt,&p)); //not include NULL character

	getch();
}
7) To achieve a program that reads from a string n words. They will be sorted alphabetically.
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>

/* There are applications where it is necessary to work with the "n" word - meaning the character 
sequence of characters that are not white. In this case we have the opportunity to declare vectors 
of words. They are, in fact, the arrays with elements of type char. */
 
void main()
{
char words[10][25];	/*each line of the 10 of the matrix can hold a string of type char *. 
			It can have up to 25 characters (including the null character). 
			Words can be addressed by a[0] (the first word) a[1] (the second word) etc.*/
	char *aux;
	int i,n,found; 
	printf("Enterthe number of words: "); 
	scanf("%d",&n);
	for(i=0;i<n;i++)
	{
		printf("word: ");
		scanf("%s",&words[i]); 
	} 
	do
	{ 
		found=0;
		for(i=0;i<n-1;i++)
			if ( strcmp(words[i],words[i+1]) > 0 )
			{
				strcpy(aux,words[i]);
				strcpy(words[i],words[i+1]);
				strcpy(words[i+1],aux);
				found=1;
			}
	} while (found); 
	for(i=0;i<n;i++)
	printf("\n%s",words[i]);
	getch();
}
Cookies help us deliver our services. By using our services, you agree to our use of cookies.