Você está na página 1de 7

1.

String length C program


#include <stdio.h>
#include <string.h>

int main()
{
char a[100];
int length;

printf("Enter a string to calculate it's length\n");


gets(a);

length = strlen(a);

printf("Length of the string = %d\n", length);

return 0;
}

String length in C without strlen


You can also find string length without strlen function. We create our function to find it. We
scan all the characters in the string if the character isn't a null character then increment the
counter by one. Once the null character is found the counter equals length of the string.

#include <stdio.h>

int main()
{
char s[1000];
int c = 0;

printf("Input a string\n");
gets(s);

while (s[c] != '\0')


c++;

printf("Length of the string: %d\n", c);

return 0;
}

2. String comparison
#include <stdio.h>
#include <string.h>

int main()
{
char a[100], b[100];

printf("Enter a string\n");
gets(a);

printf("Enter a string\n");
gets(b);

if (strcmp(a,b) == 0)
printf("The strings are equal.\n");
else
printf("The strings are not equal.\n");

return 0;
}

C string comparison program without strcmp


We can create a function to compare two strings.

#include <stdio.h>

int compare_strings(char [], char []);

int main()
{
char a[1000], b[1000];

printf("Input a string\n");
gets(a);

printf("Input a string\n");
gets(b);

if (compare_strings(a, b) == 0)
printf("Equal strings.\n");
else
printf("Unequal strings.\n");

return 0;
}

int compare_strings(char a[], char b[])


{
int c = 0;

while (a[c] == b[c]) {


if (a[c] == '\0' || b[c] == '\0')
break;
c++;
}

if (a[c] == '\0' && b[c] == '\0')


return 0;
else
return -1;
}

3. String copy C program


#include <stdio.h>
#include <string.h>

int main()
{
char source[1000], destination[1000];

printf("Input a string\n");
gets(source);

strcpy(destination, source);

printf("Source string: %s\n", source);


printf("Destination string: %s\n", destination);

return 0;
}

Copy string in C without using strcpy


#include <stdio.h>

int main()
{
int c = 0;
char s[1000], d[1000] = "What can I say about my programming skills?";

printf("Before copying, the string: %s\n", d);

printf("Input a string to copy\n");


gets(s);
while (s[c] != '\0') {
d[c] = s[c];
c++;
}

d[c] = '\0';

printf("After copying, the string: %s\n", d);

return 0;
}

C program to concatenate strings


#include <stdio.h>
#include <string.h>

int main()
{
char a[1000], b[1000];

printf("Enter the first string\n");


gets(a);

printf("Enter the second string\n");


gets(b);

strcat(a, b);

printf("String obtained on concatenation: %s\n", a);

return 0;
}

Concatenate strings without strcat function


C program to concatenate strings without using library function strcat of string.h header file.
We create our own function.

#include <stdio.h>

void concatenate(char [], char []);

int main()
{
char p[100], q[100];

printf("Input a string\n");
gets(p);

printf("Input a string to concatenate\n");


gets(q);

concatenate(p, q);

printf("String obtained on concatenation: \"%s\"\n", p);

return 0;
}

void concatenate(char p[], char q[]) {


int c, d;

c = 0;

while (p[c] != '\0') {


c++;
}

d = 0;

while (q[d] != '\0') {


p[c] = q[d];
d++;
c++;
}

p[c] = '\0';
}

4. Reverse a string in C using strrev


#include <stdio.h>
#include <string.h>

int main()
{
char arr[100];

printf("Enter a string to reverse\n");


gets(arr);

strrev(arr);

printf("Reverse of the string is \n%s\n", arr);


return 0;
}

String reversal without strrev function


First we calculate length of the string without using strlen function and then copy its
characters in reverse order (from end to beginning) to a new string using a for loop.

#include <stdio.h>

int main()
{
char s[1000], r[1000];
int begin, end, count = 0;

printf("Input a string\n");
gets(s);

// Calculating string length

while (s[count] != '\0')


count++;

end = count - 1;

for (begin = 0; begin < count; begin++) {


r[begin] = s[end];
end--;
}

r[begin] = '\0';

printf("%s\n", r);

return 0;
}

C program remove spaces, blanks from a string


#include <stdio.h>

int main()
{
char text[1000], blank[1000];
int c = 0, d = 0;

printf("Enter some text\n");


gets(text);
while (text[c] != '\0') {
if (text[c] == ' ') {
int temp = c + 1;
if (text[temp] != '\0') {
while (text[temp] == ' ' && text[temp] != '\0') {
if (text[temp] == ' ') {
c++;
}
temp++;
}
}
}
blank[d] = text[c];
c++;
d++;
}

blank[d] = '\0';

printf("Text after removing blanks\n%s\n", blank);

return 0;
}

Você também pode gostar